Android 自定义组合控件

2024-04-09  本文已影响0人  付凯强

所谓组合控件,指的是把系统现有的控件组合在一起形成一个新控件。这里我们自定义一个LinearLayout控件,LinearLayout控件中又含有RelativeLayout控件,RelativeLayout控件中有TextView控件和RadioGroup控件。布局如下:

combined_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv"
            android:layout_marginStart="10dp"
            android:layout_centerVertical="true"
            android:layout_alignParentStart="true"
            android:text="你的性别是?"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioGroup
            android:id="@+id/rg"
            android:layout_centerVertical="true"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="10dp"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/bt_01"
                android:text="男"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioButton
                android:id="@+id/bt_02"
                android:text="女"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </RadioGroup>
    </RelativeLayout>

</LinearLayout>

控件的布局有了,需要把当前布局添加到控件树中。

CustomCombinedView

创建类CustomCombinedView,继承自LinearLayout,在构造方法中进行初始化

    public CustomCombinedView(Context context) {
        this(context,null);
    }

    public CustomCombinedView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomCombinedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.combined_view, this);
        TextView textView = view.findViewById(R.id.tv);
        RadioGroup radioGroup = view.findViewById(R.id.rg);
        textView.setText("不是默认的字符串");
        textView.setTextColor(Color.RED);
        radioGroup.setOnCheckedChangeListener(this);
    }

首先通过View.inflate将当前的布局添加到控件树中去,然后通过返回的View获取布局中的子控件。特意选了RadioGroup,这里可以通过对RadioGroup的监听加一些逻辑:RadioGroup setOnCheckedChangeListener:

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.bt_01) {
            Toast.makeText(getContext(),"I am man.",Toast.LENGTH_SHORT).show();
        } else if (checkedId == R.id.bt_02) {
            Toast.makeText(getContext(),"I am women.",Toast.LENGTH_SHORT).show();
        }
    }

点击不同的RadioButton会弹不同内容的Toast。

总结

自定义组合控件大致分为以下几步:

  1. 书写布局
  2. 将布局以代码的方式添加到控件树中,并得到对应的View对象
  3. 通过View对象可以获得子控件的Id,通过Id可以自定义处理逻辑。
上一篇 下一篇

猜你喜欢

热点阅读