繁体   English   中英

如何使用右上角的十字按钮添加动态图像视图

[英]How to add dynamic image view with cross button on top right side

我想在app中提供附件功能。 请参阅附件屏幕截图。 在此处输入图片说明

如何在Android中以语法方式创建程序?

您应该使用2个imageViews创建一个frameLayout,其中一个包含实际图像,另一个包含十字。

创建包含框架布局的custom_view.xml

 <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
    ....>
    <ImageView/>
    <ImageView/>

    </FrameLayout>

然后创建一个自定义视图类,如下所示:

public class MyCustomView extends ViewGroup {
    public MyCustomView(Context context, AttributeSet attrs) {
         super(context, attrs);
         LayoutInflater inflater = LayoutInflater.from(context);
         layoutInflater.inflate(R.layout.custom_view, this);
     }
 }

如我所见,您的问题已得到解决。 首先,创建视图的XML布局。 然后创建将使用GET方法扩展的类。 然后通过代码创建此Views并添加到容器中。

layout_custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/root"
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="200dp"
             android:layout_height="150dp">

    <ImageView
        android:id="@+id/img_photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="12dp"
        android:background="#000"/>

    <ImageButton
        android:id="@+id/btn_close"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_gravity="end"
        android:background="#00000000"
        android:scaleType="fitXY"
        android:src="@drawable/temp_close"/>

</FrameLayout>

CustomView.class:

public class CustomView extends FrameLayout {

    private View mRoot;
    private ImageView mImgPhoto;
    private View mBtnClose;

    private Context mContext;

    public CustomView(final Context context) {
        this(context, null);
    }

    public CustomView(final Context context, final AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        if (isInEditMode())
            return;

        final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View customView = null;

        if (inflater != null)
            customView = inflater.inflate(R.layout.layout_custom, this);

        if (customView == null)
            return;

        mRoot = customView.findViewById(R.id.root);
        mImgPhoto = (ImageView) customView.findViewById(R.id.img_photo);
        mBtnClose = customView.findViewById(R.id.btn_close);
    }

    public View getRoot() {
        return mRoot;
    }

    public ImageView getImgPhoto() {
        return mImgPhoto;
    }

    public View getBtnClose() {
        return mBtnClose;
    }
}

以及该人员的最终用法:

final CustomView customView1 = new CustomView(getBaseContext());
final CustomView customView2 = new CustomView(getBaseContext());
final LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(customView1);
container.addView(customView2);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM