第一步创建组合控件布局文件 list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/address_picker_root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="10dp"
android:paddingLeft="10dp">
<TextView
android:id="@+id/user_info_left_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:typeface="sans"
android:textSize="14sp"
android:textColor="@color/black"/>
<ImageView
android:id="@+id/right_avatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_toLeftOf="@id/user_info_right_text"
/>
<TextView
android:id="@+id/user_info_right_text"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="25dp"
android:typeface="sans"
android:layout_toLeftOf="@id/right_into_edit_img"
android:gravity="right"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="14sp" />
<ImageView
android:id="@+id/right_into_edit_img"
android:layout_width="wrap_content"
android:layout_height="10dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
第二步创建attrs文件用来为控件设置资源属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ListitemView">
<attr name="left_text" format="string"/>
<attr name="right_avatar" format="reference"/>
</declare-styleable>
</resources>
第三步创建组合控件类,大多继承自ViewGroup类或者它的子类 ListitemView.class
public class ListitemView extends RelativeLayout {
private String left_text;
private TextView leftTextView;
private int rightAvatarImg;
private ImageView rightAvatarImgResource;
private clickListener address_picker_listener;
public ListitemView(Context context) {
this(context, null); //这样写是为了保证能通过含有三个参数的构造方法创建其对象
}
public ListitemView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);//这样写是为了保证能通过含有三个参数的构造方法创建其对象
}
public ListitemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}
public ListitemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public interface clickListener{
void addressPcikerListener();
}
public void AddClickListener(clickListener clickListener) {
this.address_picker_listener = clickListener;
}
private void init(Context context, @Nullable AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ListitemView);
left_text = typedArray.getString(R.styleable.ListitemView_left_text);
rightAvatarImg = typedArray.getResourceId(R.styleable.ListitemView_right_avatar,0);
typedArray.recycle();
LayoutInflater.from(context).inflate(R.layout.list_item, this, true);
leftTextView=findViewById(R.id.user_info_left_text);
rightAvatarImgResource=findViewById(R.id.right_avatar);
Glide.with(context).load(rightAvatarImg).into(rightAvatarImgResource);
leftTextView.setText(left_text);
}
}
第四步主活动的使用
public class MainActivity extends AppCompatActivity {
private ListitemView mListitem;
private TextView mTvLeft;
private ImageView mImgCenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取组合控件内部的控件对象
mListitem = findViewById(R.id.list_item);
mTvLeft = mListitem.findViewById(R.id.user_info_left_text);
mImgCenter = mListitem.findViewById(R.id.right_avatar);
//添加文字
mTvLeft.setText("woawdoawd");
//添加图片的方法
Glide.with(MainActivity.this).load(R.mipmap.ic_launcher).into(mImgCenter);
mListitem.AddClickListener(new ListitemView.clickListener() {
@Override
public void addressPcikerListener() {
Log.d("gaoqiang", "addressPcikerListener: ");
//暂时不好用
}
});
}
}
对于固定不变的可以在布局文件中引入attrs资源文件对它直接赋值
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:attrs="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.combinationapplication.ListitemView
android:id="@+id/list_item"
android:layout_width="match_parent"
attrs:left_text="adkjawhd"
android:layout_height="50dp"/>
</LinearLayout>
引入代码
xmlns:attrs="http://schemas.android.com/apk/res-auto"
设置属性代码
attrs:left_text="adkjawhd"