【问题标题】:How to add dynamic image view with cross button on top right side如何在右上角添加带有十字按钮的动态图像视图
【发布时间】:2016-02-08 06:42:18
【问题描述】:

我想在 app 中提供附件功能。请看附件截图。

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

【问题讨论】:

  • 但我不想在警报对话框中显示这些附件,我想在活动屏幕中显示它们

标签: android android-layout imageview


【解决方案1】:

我可以看到您的问题的解决方案。首先创建视图的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);

【讨论】:

    【解决方案2】:

    您应该创建一个带有 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);
         }
     }
    

    【讨论】:

    • 我必须动态创建框架布局?
    • 请看编辑,顺便说一句你也可以使用RelativeLayout
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    相关资源
    最近更新 更多