![效果图](https://i-blog.csdnimg.cn/blog_migrate/00522a6a6d267b7665d9e7ba277c00ad.jpeg)
1.用到的第三方库:
//知乎 图片视频选择
implementation 'com.zhihu.android:matisse:0.5.2-beta4'
2.自定义组合控件
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingLeft="12dp"
android:background="@color/colorWhite"
android:gravity="center_vertical"
android:paddingRight="12dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/imglayout_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="我是标题"
android:textColor="#ff333333"
android:textSize="15sp" />
<TextView
android:id="@+id/imglayout_upload"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="上传(0份)"
android:padding="6dp"
android:background="@drawable/img_layout"
android:textColor="#1685FF"
android:textSize="12sp" />
<TextView
android:id="@+id/imglayout_yl"
android:layout_marginLeft="7dp"
android:paddingHorizontal="15dp"
android:paddingVertical="6dp"
android:background="@drawable/img_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#1685FF"
android:text="预览"
android:textSize="13sp"
/>
</LinearLayout>
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
/**
* @author: 何处可安生
* @date: 2020/5/18
*/
public class MyImgLinearLayout extends LinearLayout {
private TextView textView_title,textView_upload,textView_yl;
private ImgListener imgListener;
private View view;
public MyImgLinearLayout(Context context) {
super(context);
}
public MyImgLinearLayout(final Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
view=LayoutInflater.from(context).inflate(R.layout.my_imglayout, this, true);
textView_title=findViewById(R.id.imglayout_title);
textView_upload=findViewById(R.id.imglayout_upload);
textView_yl=findViewById(R.id.imglayout_yl);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MyImgLinearLayout);
if (attributes != null) {
String title = attributes.getString(R.styleable.MyImgLinearLayout_my_title);
if (!TextUtils.isEmpty(title)) {
textView_title.setText(title);
}
attributes.recycle();
}
/**
* 上传图片的点击事件
*/
textView_upload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (imgListener != null) {
imgListener.onUploadListener(view);
}
}
});
/**
* 预览的点击事件
*/
textView_yl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (imgListener!=null){
imgListener.onYlImgListener(view);
}
}
});
}
public interface ImgListener{
/**
* 添加图片
*/
void onUploadListener(View v);
/**
* 预览图片
*/
void onYlImgListener(View v);
}
public void setUploadClickListener(ImgListener imgListener) {
this.imgListener=imgListener;
}
}
Activity中使用:
<com.xht.rz.views.MyImgLinearLayout
android:id="@+id/yscheck_sfzrx"
android:layout_width="match_parent"
android:layout_height="47dp"
app:my_title="身份证人像面" />
<com.xht.rz.views.MyImgLinearLayout
android:id="@+id/yscheck_sfzgh"
android:layout_width="match_parent"
android:layout_height="47dp"
app:my_title="身份证国徽面" />
yscheckSfzrx.setUploadClickListener(new MyImgLinearLayout.ImgListener() {
@Override
public void onUploadListener(View v) {
//上传图片
}
@Override
public void onYlImgListener(View v) {
//预览图片
}
});