Do the project will certainly encounter such a requirement, the data load is empty or the data load exception when displaying an empty page to the user:
This blog is to implement such a simple package
1. First define a linear layout
2. Then use a tool class to manage such a view
First look at the custom linear layout
Public class emptylayout extends linearlayout{ /** * Empty page picture * * PrivateImageView emptyimg;/** * Empty page text * / PrivateTextView Emptytv;/** * Empty text * * PrivateString Emptytext;/** * Empty text color * / Private intEmptytextcolor;/** * Empty Text Size * / Private floatEmptytextsize;/** * Empty picture * * PrivateDrawable emptydrawable;/** * Picture width * / Private intImgWidth;/** * Picture Height * / Private intImgHeight; Public Emptylayout(Context context) { This(Context,NULL); } Public Emptylayout(context context, AttributeSet attrs) { This(Context, Attrs,0); } Public Emptylayout(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr);//Initialize layoutInitview ();//Initialization ParametersInitdatas (); }/** * Initializing layouts * / Private void Initview() {View view= layoutinflater.from (GetContext ()). Inflate (R.layout.empty_layout, This);//Note the parent incoming linearlayout of the inflate methodEmptyimg= (ImageView) View.findviewbyid (R.ID.COMMON_IV); emptytv= (TextView) View.findviewbyid (R.ID.COMMON_TV); }/** * Set text * * Public void Setemptytext(String Emptytext) { This. Emptytext=emptytext;if(emptytext!=NULL) Emptytv.settext (Emptytext); }/** * Set Text color * / Public void Setemptytextcolor(intEmptytextcolor) { This. Emptytextcolor=emptytextcolor; Emptytv.settextcolor (Emptytextcolor); }/** * Set empty text font size * * @param emptytextsize Unit is SP */ Public void setemptytextsize(floatEmptytextsize) { This. emptytextsize=emptytextsize; Emptytv.settextsize (typedvalue.complex_unit_px,emptytextsize); }/** * Setting picture * / @TargetApi(Build.version_codes. Jelly_bean) Public void setemptydrawable(drawable drawable) { This. emptydrawable=drawable; Emptyimg.setbackground (drawable); }/** * Set the width and height of the picture */ Public void Setimgwh(intImgWidth,intImgHeight) { This. imgheight=imgheight; This. imgwidth=imgwidth; Viewgroup.layoutparams Lp=emptyimg.getlayoutparams (); Lp.width=imgwidth; Lp.height=imgheight; EMPTYIMG.SETLAYOUTPARAMS (LP); }/** * Initialization parameters * / Public void Initdatas() {setemptytext (emptytext); Setemptytextcolor (Emptytextcolor); Setemptytextsize (emptytextsize); Setemptydrawable (emptydrawable); Setimgwh (Imgwidth,imgheight); }}
The code is very simple, simple I do not know what to say, the comments are more clear.
The layout is then initialized with a tool class:
public class Viewutil {public static emptylayout Getemptylayout (context context, String text) {emptylayout Layo Ut=new emptylayout (context);Layout. Setemptytext(text);Layout. Setemptytextsize(commonutils. Dip2PX (Context, the));Layout. Setemptytextcolor(Context. Getresources(). GetColor(R. Color. Coloraccent));Layout. Setemptydrawable(Context. Getresources(). Getdrawable(R. Mipmap. NoData));Layout. Setimgwh(commonutils. Dip2PX (Context, the), Commonutils. Dip2PX (Context, -));It controls the Abslistview.. Layoutparamsparams = new Abslistview. Layoutparams(Abslistview. Layoutparams. MATCH_parent, Commonutils. Dip2PX (Context, -));Layout. Setlayoutparams(params);return layout;}}
There is no difficulty in using a DP-to-PX approach, which is common:
/** * dp转px */ publicstaticintdip2px(Context context ,float dpValue){ float density=context.getResources().getDisplayMetrics().density; return (int)(dpValue*density+0.5); }
Finally see how to use:
publicclass MainActivity extends AppCompatActivity { @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(ViewUtil.getEmptyLayout(this,"暂无数据")); }}
Just call where you want to use the view.
ViewUtil.getEmptyLayout(this,"暂无数据")
The same can be similar to the implementation of the Web-free condition of the page, page loading failed pages and so on. Instead of having to write the layout file in the layout of the corresponding page.
PS: In the end, put a way to the login or not package
/** * Prompt login * @param context * @param clazz */ Public Static void isenterloginact(FinalContext Context,class clazz) {//Get login user to save data BooleanIslogin=textutils.isempty (Globalparams.token)if(IsLogin) {Intent Intent =NewIntent (context, clazz); Context.startactivity (Intent); }Else{Showdialogutils.showinfodialog (context,"You are not logged in, are you going to log in?" ",NewView.onclicklistener () {@Override Public void OnClick(View v) {Intent Intent =NewIntent (context, loginact.class); Context.startactivity (Intent); } }); } }
has been the package is not very good, do not accumulate silicon step without even thousands of miles, first from the simplest to start.
Android Display empty Page tool class