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) {
//預覽圖片
}
});