Android——如何构建一个布局,当一个组件隐藏时,另一个组件可以覆盖它
要求是:Android——如何构建一个布局,当一个组件隐藏时,另一个组件可以覆盖它,android,android-layout,Android,Android Layout,要求是: 有一个listitem,我想添加两个组件(一个imageview,一个 复选框)添加到它 在默认情况下,当我长按 listitem中,imageview设置为不可见,复选框可见 我想以一种适当的方式构建listitem的布局,我尝试了由这两个元素组成的relativelayout,并设置了这两个元素android:visibility=gone,但它不起作用。 我想问题存在于以下几个方面: relativelayout的字段是错误的,那么如何编写它呢 是否应该尝试其他布局,如fra
- 有一个listitem,我想添加两个组件(一个imageview,一个 复选框)添加到它
- 在默认情况下,当我长按 listitem中,imageview设置为不可见,复选框可见
android:visibility=gone
,但它不起作用。
我想问题存在于以下几个方面:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="72dp"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<ImageButton
android:id="@+id/ibClinicMessageLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
/>
<!-- item中的text,subtext和时间 -->
<LinearLayout
android:id="@+id/llTotalText"
android:layout_toRightOf="@id/ibClinicMessageLeft"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvClinicMessagetTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/ibClinicMessageLeft"
android:layout_centerVertical="true"
android:typeface="sans"
android:textColor="@color/text"
android:textSize="16sp" />
<TextView
android:id="@+id/tvClinicMessageTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="20dp"
android:layout_centerVertical="true"
android:typeface="sans"
android:textColor="@color/hint"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:id="@+id/tvClinicMessageText"
android:ellipsize="marquee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="6dp"
android:layout_marginLeft="16dp"
android:layout_below="@id/tvClinicMessagetTitle"
android:layout_toRightOf="@+id/ibClinicMessageLeft"
android:typeface="sans"
android:textColor="@color/subtext"
android:textSize="14sp" />
</LinearLayout>
<RelativeLayout
android:layout_toRightOf = "@id/llTotalText"
android:layout_width ="wrap_content"
android:layout_height ="fill_parent"
>
< CheckBox
android:id ="@+id/cbCheckBox"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:visibility ="gone"
android:clickable ="false"
android:focusable ="false"
/>
<!-- item中的箭头图片 -->
< ImageView
android:id ="@+id/ivItemArrow"
android:src ="@drawable/im_arrow"
android:layout_width ="wrap_content"
android:layout_height ="fill_parent"
android:layout_centerVertical ="true"
android:layout_marginRight ="16dp"
android:visibility ="gone"
/>
</ RelativeLayout>
</LinearLayout>
<复选框
android:id=“@+id/cbCheckBox”
android:layout\u width=“包装内容”
android:layout\u height=“包装内容”
android:visibility=“消失”
android:clickable=“false”
android:focusable=“false”
/>
在这里,您要做的是将imageview的可见性设置为VISIBLE,而不是在列表视图中添加任何复选框
然后在运行时,在列表Listener中,将imageview的可见性设置为不可见,并实例化一个复选框,将其设置为可见,并将其放置在所需位置
大概是这样的:
CheckBox edt;
edt = new CheckBox(this);
RelativeLayout.LayoutParams lp =
new RelativeLayout.LayoutParams
(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
);
lp.setMargins(25, 0, 0, 0); // move 25 px to right (increase left margin)
edt.setLayoutParams(lp); // lp.setMargins(left, top, right, bottom);
ImageView iv = (ImageView)findViewById(R.id.iv1);
RelativeLayout.LayoutParams newparam = iv.getLayoutParams();
您可以将边距设置为imageview使用的任何边距
像这样:
CheckBox edt;
edt = new CheckBox(this);
RelativeLayout.LayoutParams lp =
new RelativeLayout.LayoutParams
(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
);
lp.setMargins(25, 0, 0, 0); // move 25 px to right (increase left margin)
edt.setLayoutParams(lp); // lp.setMargins(left, top, right, bottom);
ImageView iv = (ImageView)findViewById(R.id.iv1);
RelativeLayout.LayoutParams newparam = iv.getLayoutParams();
就是这样。由于复选框没有添加到列表视图中,我对“checkbox edt=(checkbox)findViewById(R.id.yourcheckbox);”有点困惑,该id来自哪里?实际上,listitem还包含另外3个组件(一个大的线性布局包裹整个布局,imageview和复选框位于水平方向的第3位)发布布局文件。id来自布局文件。您的复选框必须设置如下id:android:id=“@+id/checkbox”我已经放置了布局~~您仍然没有得到答案吗?复选框id为:cbCheckBox,ImageView id为:ibClinicMessageLeft将ImageView设置为不可见,并使用相同的布局参数将复选框设置为可见,以便将其放置在ImageView所在的确切位置。