Android 如何添加右上角带有十字按钮的动态图像视图
我想在应用程序中提供附件功能。请参阅附加屏幕截图Android 如何添加右上角带有十字按钮的动态图像视图,android,android-layout,imageview,Android,Android Layout,Imageview,我想在应用程序中提供附件功能。请参阅附加屏幕截图 如何在Android中创建专业语法 您应该创建一个包含两个ImageView的frameLayout,一个包含实际图像,另一个包含十字 创建包含框架布局的自定义_view.xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout ....> <ImageView/> <ImageView/> </
如何在Android中创建专业语法 您应该创建一个包含两个ImageView的frameLayout,一个包含实际图像,另一个包含十字 创建包含框架布局的自定义_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
....>
<ImageView/>
<ImageView/>
</FrameLayout>
正如我所看到的,你问题的解决办法。首先创建视图的
XML
布局。然后创建类,该类将使用GET
方法膨胀到它中。然后通过代码创建此视图
,并添加到容器中
layout_custom.xml:
最后,该员工的使用:
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);
但我不想在警报对话框中显示这些附件,我想在活动屏幕中显示它们我必须动态创建框架布局?请查看编辑,您也可以使用RelativeLayout btw
<?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>
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);