自定义组合控件textview

本文详细介绍了一种自定义Android组合控件的方法,包括XML布局文件、属性设置、Java类编写及在页面中的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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


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

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--自定义控件的属性-->
    <declare-styleable name="boshu">
        <attr name="boshuText" format="string"/>
        <attr name="boshuBg1" format="integer"/>
        <attr name="boshuBg2" format="integer"/>
        <attr name="boshuBg3" format="integer"/>
    </declare-styleable>
</resources>

3.自定义控件java文件:myview.java 继承的布局要与myview.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);
    }
}

4.调用该自定义控件的xml文件:second_main.xml:

<TableLayout 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">


    <com.minzu.uidesign.boshu
        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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值