【发布时间】:2014-02-19 03:43:42
【问题描述】:
我的相对布局中有两个视图 - TextView 和 ImageView。这些是我想要的结果:
1) 如果 imageview 被隐藏(如果满足某些条件,它可以通过程序设置为 visibility=gone),则 textview 在相对布局中垂直和水平居中对齐
2) 如果 imageview 未隐藏,则 textview 应水平居中对齐,但垂直占用的空间不超过所需空间 - imageview 占据所有可用高度。
这是我正在使用的代码。但它不起作用,因为如果我将 imageview 设置为 fill_parent 它会与文本重叠,如果我将它设置为 wrap_content 它太小了:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_above="@+id/linearlayout_buttons">
<TextView
android:id="@+id/text_compliance_question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="22sp"
android:layout_alignWithParentIfMissing="true"
android:layout_above="@+id/image_thumbnail"
android:gravity="center"
android:textStyle="bold" />
<ImageView
android:visibility="visible"
android:id="@+id/image_thumbnail"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:contentDescription="@string/title_activity" />
</RelativeLayout>
感谢任何想法。
【问题讨论】:
-
使用RelativeLayout有什么特别的原因吗?带有 weightSum 的 LinearLayout 可能会满足您的需求。
-
感谢您的回复。使用RelativeLayout 没有特别的原因。如果我使用权重,它不会变成百分比游戏吗?我不需要按百分比完成,我只想让事情尽可能多地占用空间。
-
使用重量并不一定意味着它是按百分比完成的。将 weightSum 设置为“1”,ImageView 的权重设置为“1”,ImageView 将尽可能多地占用空间。
标签: android android-relativelayout vertical-alignment