Today we talk about the problem of custom control, today, this is from the layout of the custom start, the difficulty is not very difficult, a look at it, estimated that some students or developers to see that this way superfluous, but small series I do not think so, more than a solution, more than a extrapolate of learning. Next time, or in a few days, I'm going to start with a custom property, using attributes in a layout file, and I want to be able to help you with an article about custom controls.
now it's time to start customizing the implementation of Imagetextbutton with pictures and text.
As follows:
First step: Create a new Image_text_buttton.xml layout file for your custom control to use
[HTML]View PlainCopy
- <? XML version="1.0" encoding="Utf-8"?>
- < LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
- android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:orientation="vertical" >
- < ImageView
- android:id="@+id/iv"
- android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:layout_gravity="Center_horizontal"
- android:layout_margintop="12DP" />
- < TextView
- android:id="@+id/tv"
- android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:layout_gravity="Center_horizontal"
- android:layout_margintop="8DP"
- Android:textcolor="#000000" />
- </ LinearLayout >
Step Two: Customize a class Imagetextbutton inherit LinearLayout
[Java]View PlainCopy
- Package Net.loonggg.itbd.view;
- Import NET.LOONGGG.ITBD.R;
- Import Android.content.Context;
- Import Android.util.AttributeSet;
- Import Android.view.LayoutInflater;
- Import Android.widget.ImageView;
- Import Android.widget.LinearLayout;
- Import Android.widget.TextView;
- public class Imagetextbutton extends LinearLayout {
- Private ImageView IV;
- Private TextView TV;
- Public Imagetextbutton (Context context) {
- Super (context);
- }
- Public Imagetextbutton (context context, AttributeSet Attrs) {
- Super (context, attrs);
- Layoutinflater.from (context). Inflate (R.layout.image_text_buttton, this,
- true);
- IV = (ImageView) Findviewbyid (R.ID.IV);
- TV = (TextView) Findviewbyid (r.id.tv);
- }
- public void Setdefaultimageresource (int resId) {
- Iv.setimageresource (RESID);
- }
- public void Setdefaulttextviewtext (String text) {
- Tv.settext (text);
- }
- /**
- * @param resId
- */
- public void Setimageresource (int resId) {
- Iv.setimageresource (RESID);
- }
- /**
- * @param text
- */
- public void Settextviewtext (String text) {
- Tv.settext (text);
- }
- /**
- * @param color
- */
- public void SetTextColor (int color) {
- Tv.settextcolor (color);
- }
- }
Step Three: Use of custom controls in layout file Activity_main.xml
[HTML]View PlainCopy
- < Relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"
- xmlns:tools="Http://schemas.android.com/tools"
- android:layout_width="Match_parent"
- android:layout_height="Match_parent"
- Android:paddingbottom="@dimen/activity_vertical_margin"
- android:paddingleft="@dimen/activity_horizontal_margin"
- android:paddingright="@dimen/activity_horizontal_margin"
- android:paddingtop="@dimen/activity_vertical_margin"
- Tools:context=". Mainactivity " >
- < Net.loonggg.itbd.view.ImageTextButton
- android:id="@+id/itb"
- android:layout_width="Wrap_content"
- android:layout_height="wrap_content" />
- </ Relativelayout >
Fourth step: use in Activiy
[Java]View PlainCopy
- public class Mainactivity extends Activity {
- Private Imagetextbutton ITB;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- ITB = (Imagetextbutton) Findviewbyid (R.ID.ITB);
- Itb.setimageresource (r.drawable.sure);
- Itb.settextviewtext ("OK");
- }
- }
Customize Imagetextbutton with pictures and text