Android自定义控件系列(1):选项控件

一、控件效果展示

二、控件类:OptionsItemView.java

/**
 * 选项Item
 * 
 * @author : ZGS
 * Created on 2021/11/16
 **/
public class OptionItemView extends ConstraintLayout {
    private TextView tvLeft;
    private TextView tvRight;
    private ImageView ivRight;
    private View line;

    public OptionItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public OptionItemView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    public OptionItemView(@NonNull @NotNull Context context) {
        super(context);
        init(context,null);
    }

    private void init(Context context, AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.view_options_item,this);
        tvLeft = findViewById(R.id.tvLeft);
        tvRight = findViewById(R.id.tvRight);
        ivRight = findViewById(R.id.ivRight);
        line = findViewById(R.id.line);
        TypedArray params = context.obtainStyledAttributes(attrs, R.styleable.OptionItemView);
        String tvLeftText = params.getString(R.styleable.OptionItemView_tvLeftText);
        String tvRightText = params.getString(R.styleable.OptionItemView_tvRightText);
        Integer ivRightRes = params.getResourceId(R.styleable.OptionItemView_ivRight,R.drawable.ic_right);

        setTvLeftText(tvLeftText);
        setTvRightText(tvRightText);
        setIvRightResource(ivRightRes);

        params.recycle();
    }
    public void setOperationListener(OnClickListener listener) {
        ivRight.setOnClickListener(listener);
    }

    public void setTvLeftText(String text){
        tvLeft.setText(text);
    }
    public void setTvRightText(String text){
        tvRight.setText(text);
    }
    public void setIvRightResource(Integer rId){
        ivRight.setImageResource(rId);
    }
}

三、控件布局:view_options_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--左边文本-->
    <TextView
        android:id="@+id/tvLeft"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:gravity="left|center_vertical"
        android:text="TextView"
        android:textColor="@color/black"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tvRight"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!--右边文本-->
    <TextView
        android:id="@+id/tvRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/ivRight"
        app:layout_constraintTop_toTopOf="parent" />

    <!--右边按钮-->
    <ImageView
        android:id="@+id/ivRight"
        android:layout_width="40dp"
        android:layout_height="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_right" />

    <!--分割线-->
    <View
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginLeft="16dp"
        android:background="@color/teal_200"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

四、使用控件:activity_user.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.user.UserActivity">

    <com.sziit.bluetoothassistant.view.OptionItemView
        android:id="@+id/option1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        app:tvLeftText="个人信息"
        app:tvRightText="编辑" />


    <com.sziit.bluetoothassistant.view.OptionItemView
        android:id="@+id/option2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        app:tvLeftText="修改密码"
        app:tvRightText="编辑" />
</androidx.appcompat.widget.LinearLayoutCompat>

五、控件点击处理:UserActivity.java

public class UserActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        
        OptionItemView option1 = findViewById(R.id.option1);
        option1.setOperationListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO 在这里添加点击处理
                Toast.makeText(UserActivity.this, "点击处理", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值