自定义组件(二)------使用系统控件组合的自定义控件

自定义组件(二)—使用系统控件组合的自定义控件

——ONE Goal ,ONE Passion!


今天再写一篇简单的关于自定义控件的文章,有助于自己来理解自定义控件的思想.下面开始学习自定义组合控件的学习!


1.将我们想要的控件使用xml文件组合出来

在layout文件夹下创建setting_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="30dp"
    android:layout_margin="10dp"
    android:orientation="horizontal">

    <TextView
        android:clickable="true"
        android:id="@+id/tv_left"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="返回"
        android:textSize="20dp"/>
    <TextView
        android:clickable="true"
        android:id="@+id/tv_center"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text="中心"
        android:textSize="20dp"/>
    <TextView
        android:clickable="true"
        android:id="@+id/tv_right"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="右边"
        android:textSize="20dp"/>


</LinearLayout>
2.将布局加载到自定义的view中并注册一些事件
public class SettingView extends FrameLayout implements View.OnClickListener {

    View inflate;//布局文件
    TextView tv_left, tv_center, tv_right;

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

    public SettingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        inflate = LayoutInflater.from(context).inflate(R.layout.setting_view, null);
        //找到布局中的控件
        tv_left = (TextView) inflate.findViewById(R.id.tv_left);
        tv_center = (TextView) inflate.findViewById(R.id.tv_center);
        tv_right = (TextView) inflate.findViewById(R.id.tv_right);

        tv_left.setOnClickListener(this);
        tv_center.setOnClickListener(this);
        tv_right.setOnClickListener(this);


        //  this.addView(inflate, LinearLayout.LayoutParams.MATCH_PARENT);
        //将布局加载到自定义的控件中
        this.addView(inflate);
    }
    //一些方法来设置某些控件的属性
    public void setLeft(String str, int color) {
        tv_left.setText(str);
        tv_left.setBackgroundColor(color);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_left:
                mOnClickItem.left_clicked();

                break;
            case R.id.tv_center:
                mOnClickItem.center_clicked();

                break;
            case R.id.tv_right:
                mOnClickItem.right_clicked();

                break;
        }
    }

    public void setOnClickItemListener(OnClickItem onClickItem) {
        this.mOnClickItem = onClickItem;
    }

    /**
     * 定义一个接口增加拓展性.在外部来出来事件
     */
    public OnClickItem mOnClickItem;

    public interface OnClickItem {
        void left_clicked();

        void center_clicked();

        void right_clicked();
    }

}
3.在activity中使用控件
a. 在activity中使用的xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.custom.activity.CustomActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.example.custom.view.SettingView
            android:id="@+id/settingView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </com.example.custom.view.SettingView>
    </LinearLayout>

</RelativeLayout>
b.在activity中使用
public class CustomActivity extends Activity {

    SettingView mSettingView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom);
        mSettingView = (SettingView) findViewById(R.id.settingView);
        //设置点击控件所响应的事件
        mSettingView.setOnClickItemListener(new SettingView.OnClickItem() {
            @Override
            public void left_clicked() {
                Log.d("fy", "点击了左边");
                finish();
            }

            @Override
            public void center_clicked() {
                Log.d("fy", "点击了中间");
            }

            @Override
            public void right_clicked() {
                Log.d("fy", "点击了右边");
            }
        });

    }
}
这是一个很简单的自定义控件.我只是写出了一般自定义组合控件的主要步骤,根据这写步骤一般能绘制出自己想要的控件.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值