一、控件效果展示
二、控件类:OptionsItemView.java
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) {
Toast.makeText(UserActivity.this, "点击处理", Toast.LENGTH_SHORT).show();
}
});
}
}
|