android自定义组合控件之

自定义组合控件的界面:boshu.xml

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    
                    android:id="@+id/textview"
            android:textSize="30sp"
            android:text="@string/boshu1"
            />
                    android:id="@+id/bo1"
            android:background="@drawable/greenb"
            android:layout_width="80dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            />

                    android:id="@+id/bo2"
            android:background="@drawable/greyb"
            android:layout_width="80dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            />
                    android:id="@+id/bo3"
            android:background="@drawable/greyb"
            android:layout_width="80dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            />
                    android:id="@+id/bo4"
            android:background="@drawable/greyb"
            android:layout_width="80dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            />
    


自定义控件的属性设置,用来对自定义控件中的子控件进行设置attrs.xml

xml version="1.0" encoding="utf-8"?>

    
    name="boshu">
        name="boshuText" format="string"/>
        name="boshuBg1" format="integer"/>
        name="boshuBg2" format="integer"/>
        name="boshuBg3" format="integer"/>
        name="boshuBg4" format="integer"/>
    

自定义控件java文件:boshu.java 继承的布局要与boshu.xml定义的布局一致,这里都是TableLayout

public class boshu extends TableLayout {

    private TextView textview;
    private ImageView img1;
    private ImageView img2;
    private ImageView img3;
    private ImageView img4;

    private String boshuText;
    private Integer pic1;
    private Integer pic2;
    private Integer pic3;
    private Integer pic4;

    public boshu(Context context, AttributeSet attrs) {
        super(context, attrs);

        //加载视图布局
        LayoutInflater.from(context).inflate(R.layout.boshu,this,true);

        //加载自定义的属性
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.boshu);
        boshuText=a.getString(R.styleable.boshu_boshuText);
        pic1=a.getResourceId(R.styleable.boshu_boshuBg1,R.drawable.greyb);
        pic2=a.getResourceId(R.styleable.boshu_boshuBg2,R.drawable.greyb);
        pic3=a.getResourceId(R.styleable.boshu_boshuBg3,R.drawable.greyb);
        pic4=a.getResourceId(R.styleable.boshu_boshuBg4,R.drawable.greyb);

        //回收资源
        a.recycle();

    }

    //此方法会在所有控件都从xml文件中加载完成后调用,实现“获取子控件对象"功能
    protected void onFinishInflate(){
        super.onFinishInflate();

        //获取子控件
        textview = (TextView)findViewById(R.id.textview);
        img1=(ImageView)findViewById(R.id.bo1);
        img2=(ImageView)findViewById(R.id.bo2);
        img3=(ImageView)findViewById(R.id.bo3);
        img4=(ImageView)findViewById(R.id.bo4);

        //将从资源文件中加载的属性设置给子控件
        if(!TextUtils.isEmpty(boshuText))
        {
            setPageBoshuText(boshuText);
        }
        setPageImage1(pic1);
        setPageImage2(pic2);
        setPageImage3(pic3);
        setPageImage4(pic4);
    }
    public void setPageBoshuText(String text){
        textview.setText(text);
    }
    public void setPageImage1(int resId){
        img1.setBackgroundResource(resId);
    }
    public void setPageImage2(int resId){
        img2.setBackgroundResource(resId);
    }
    public void setPageImage3(int resId){
        img3.setBackgroundResource(resId);
    }
    public void setPageImage4(int resId){
        img4.setBackgroundResource(resId);
    }
}
调用该自定义控件的xml文件:second_main.xml

xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

            android:id="@+id/boshu1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        app:boshuBg1="@drawable/greenb"
        app:boshuBg2="@drawable/greenb"
        app:boshuBg3="@drawable/greyb"
        app:boshuBg4="@drawable/greyb"
        />
/TableLayout>

你可能感兴趣的:(android自定义组合空间)