I have been doing Android development for five years, and I did stop and stop during this period (to do back-end development and server management). When I came back to work on Android, I found that I was very unfamiliar. Many controls were written very easily before, but now I seem to have forgotten something. I always have to open this project and that project, and sometimes I may not be able to find it.
In summary, it still comes from not building a code base well, encapsulating some custom controls or modules with low coupling in development projects, or writing less blogs in daily life. If you are a programmer who has just learned to develop, or a big bird with several years of development experience, you should start sorting out your own code. It is not in vain for your life to type code. At the same time, it will also bring you a lot of impression points during the interview. So, I also began to prepare my own code base and put it on github or Weibo, hoping to communicate more with all the great masters. Next, I will put one or two custom controls first.
Customize a set of Dialog universal prompt boxes:
The appeal prompt boxes are all of a type. Of course, it may be that you are not very satisfied, or it may be inconsistent with the style required by your designer. It doesn’t matter. You just need to go in and modify the layout of the dialog. Of course, I hope that when customizing these controls, I can use XML to render them, and try not to use pictures as backgrounds or the like. The prompt box style of each app is actually generally the same. The prompt box of each page will not be different. If the change is really too big, we can just re-customize a dialog. For the rest, just set the information:
new CommomDialog(mContext, , "Are you sure you delete this information?", new () { @Override public void onClick(boolean confirm) { if(confirm){ (this,"Click OK", Toast.LENGTH_SHORT).show(); (); } } }) .setTitle("hint").show();
Let’s first look at the CommomDialog class. When defining this class, try to make the methods in it chain as much as possible to facilitate later calls.
public class CommomDialog extends Dialog implements { private TextView contentTxt; private TextView titleTxt; private TextView submitTxt; private TextView cancelTxt; private Context mContext; private String content; private OnCloseListener listener; private String positiveName; private String negativeName; private String title; public CommomDialog(Context context) { super(context); = context; } public CommomDialog(Context context, int themeResId, String content) { super(context, themeResId); = context; = content; } public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener) { super(context, themeResId); = context; = content; = listener; } protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); = context; } public CommomDialog setTitle(String title){ = title; return this; } public CommomDialog setPositiveButton(String name){ = name; return this; } public CommomDialog setNegativeButton(String name){ = name; return this; } @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.dialog_commom); setCanceledOnTouchOutside(false); initView(); } private void initView(){ contentTxt = (TextView)findViewById(); titleTxt = (TextView)findViewById(); submitTxt = (TextView)findViewById(); (this); cancelTxt = (TextView)findViewById(); (this); (content); if(!(positiveName)){ (positiveName); } if(!(negativeName)){ (negativeName); } if(!(title)){ (title); } } @Override public void onClick(View v) { switch (()){ case : if(listener != null){ (this, false); } (); break; case : if(listener != null){ (this, true); } break; } } public interface OnCloseListener{ void onClick(Dialog dialog, boolean confirm); } }
Customize the listening event, set the message, return the handle, return this;
Check out the .dialog_commom xml file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_round_white" android:orientation="vertical" > <TextView android: android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:padding="12dp" android:layout_marginTop="12dp" android:text="hint" android:textSize="16sp" android:textColor="@color/black"/> <TextView android: android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center_horizontal" android:lineSpacingExtra="3dp" android:layout_marginLeft="40dp" android:layout_marginTop="20dp" android:layout_marginRight="40dp" android:layout_marginBottom="30dp" android:text="Successfully checked in, get 200 points" android:textSize="12sp" android:textColor="@color/font_common_1"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/commom_background"/> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android: android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_dialog_left_white" android:layout_weight="1.0" android:gravity="center" android:text="@string/cancel" android:textSize="12sp" android:textColor="@color/font_common_2"/> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/commom_background"/> <TextView android: android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_dialog_right_white" android:gravity="center" android:layout_weight="1.0" android:text="@string/submit" android:textSize="12sp" android:textColor="@color/font_blue"/> </LinearLayout> </LinearLayout>
I used rounded corners throughout the background so that it doesn't look particularly stiff android:background="@drawable/bg_round_white"
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:andro android:shape="rectangle"> <solid android:color="@color/white" /> <corners android:radius="8dp" /> </shape>
Of course, the two buttons at the bottom also need to be rounded accordingly:
Lower left button: android:background="@drawable/bg_dialog_left_white"
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:andro android:shape="rectangle"> <solid android:color="@color/white" /> <corners android:bottomLeftRadius="8dp" /> </shape>
Lower right button: android:background="@drawable/bg_dialog_right_white"
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:andro android:shape="rectangle"> <solid android:color="@color/white" /> <corners android:bottomRightRadius="8dp" /> </shape>
The displayed style should also be set:
<style name="dialog" parent="@android:style/"> <item name="android:windowFrame">@null</item> <!--frame--> <item name="android:windowIsFloating">true</item> <!--Does it appearactivityAbove--> <item name="android:windowIsTranslucent">false</item> <!--translucent--> <item name="android:windowNoTitle">true</item> <!--Untitled--> <item name="android:windowBackground">@android:color/transparent</item> <!--Transparent background--> <item name="android:backgroundDimEnabled">true</item> <!--Vague--> </style>
This is basically done. By setting the message header, information body, button name, and click event, you can control your prompt box at will.
Source code link:/xiaoxiaoqingyi/mine-android-repository
Later, I will put all my code bases on and learn with you.
The above is the Android customization set of Dialog universal prompt boxes (code library) introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!