【发布时间】:2019-04-22 05:24:04
【问题描述】:
我有一个包含 2 个按钮的 LinearLayout 的 RelativeLayout。我想让这些按钮可见,而不会使整个 RelativeLayout 或 LinearLayout 可见。调试时,按钮的 getVisibility 报告可见,getLocationOnScreen 返回正确的位置,但我看不到它们。这是我的xml
<RelativeLayout
android:id="@+id/edit_div_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="20dp"
android:background="@drawable/full_popup_element"
android:layout_alignParentBottom="true"
android:visibility="gone"
android:layout_centerVertical="true"
android:clickable="false">
<ImageView
android:id="@+id/close_edit"
android:src="@drawable/view_edge_promotion_close_icon"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/uniform_style_blue"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="-10dp"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/close_edit"
android:orientation="horizontal" >
<TextView
android:id="@+id/popup_name_label"
android:text="@string/popup_name_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="@dimen/font_size_fixed_16" />
<EditText
android:id="@+id/popup_name"
android:hint=""
android:layout_width="170dp"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:singleLine="true"
android:textSize="18sp"
android:layout_marginLeft="15dp"
android:paddingBottom="2dp"
android:paddingLeft="@dimen/marginSmall" />
</LinearLayout>
<TextView
android:id="@+id/popup_assignment_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/edit_assignment_label"
android:layout_below="@id/popup_name_container"
android:layout_marginTop="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal"
android:layout_below="@id/popup_assignment_label"
android:gravity="bottom"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/popup_assignment_recycler"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"/>
<Button
android:id="@+id/btn_1"
android:text="@string/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/popup_assignment_recycler"
android:layout_alignParentRight="true"
android:gravity="bottom"
android:padding="6dp"
android:maxLines="1"
android:background="@color/uniform_style_blue"
android:textColor="#FFFFFF"
android:layout_marginLeft="18dp"
/>
<Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn_1"
android:layout_alignParentRight="true"
android:gravity="bottom"
android:padding="6dp"
android:maxLines="1"
android:background="@color/uniform_style_blue"
android:textColor="#FFFFFF"
android:layout_marginLeft="9dp"
/>
</LinearLayout>
</RelativeLayout>
我正在使用
设置可见性Button mBtn1 = findViewById(R.id.btn_1);
mBtn1.setVisibility(View.VISIBLE);
Button mBtn2 = findViewById(R.id.btn_2);
mBtn2.setVisibility(View.VISIBLE);
我也尝试在 xml 中将 RelativeLayout 的可见性定义为 INVISIBLE,但仍然没有运气。
是否可以只显示按钮?我很难过,所以任何帮助表示赞赏!
【问题讨论】:
-
不可能有一个不可见的父 ViewGroup 和一个可见的子 View。所以你需要重新设计你的布局,或者(如果你保持原样)你必须单独切换 ImageView、RecyclerView、popup_assignment_label TextView 和包含 EditText 的 LinearLayout 的可见性
-
无赖,但很高兴知道,谢谢!是否可以将按钮视图放在布局之外,并将它们放在屏幕上相对于它们现在所在的相对布局的固定位置?
-
RelativeLayout 的高度会有所不同,例如随着 RecyclerView 中项目的数量和/或大小,按钮的位置(似乎与 RecyclerView 的底线对齐)也必须有所不同(否则您可以简单地为纽扣)。您可以尝试跟踪 RelativeLayout 大小的变化,并且每次都相应地设置 Button 位置,但是 IMO 这比根据应用程序的要求简单地切换六个不同视图的可见性要麻烦得多业务逻辑。
-
我明白了。我的 recyclerview 是水平的并且有一个固定的宽度,所以我认为这可能很简单。我对切换可见性的担忧是,是否所有这些元素都会显示出来,即使是很短的时间?为了清楚起见,您建议调用 relativelayout.setVisibility(VISIBLE),然后将其所有子视图切换到 GONE?再次感谢您的帮助 - 我已经被困在这个问题上太久了!
-
如果您正在谈论将所有子视图设置为 GONE,我想知道按钮真正应该显示在哪里。 (到现在为止,我认为您希望它们具有 180 dp 的右边距和一个取决于 RecyclerView 的 y 位置。)也许一张图片将有助于阐明您想要实现的目标?
标签: java android xml view visibility