【问题标题】:Vertically aligning components of a RelativeLayout垂直对齐 RelativeLayout 的组件
【发布时间】: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


【解决方案1】:

这是你的布局,希望这是你要找的

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_margin="20dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <LinearLayout
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="0dp">
            <TextView
                    android:gravity="center"
                    android:text="hello there"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            <ImageView
                    android:visibility="gone"
                    android:layout_weight="1"
                    android:src="@drawable/ic_launcher"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <Button
                android:text="dynamic button 1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        <Button
                android:text="dynamic button 2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        <Button
                android:text="dynamic button 3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
            android:layout_marginTop="20dp"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <Button
                android:text="dynamic button 2"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <Button
                android:layout_weight="1"
                android:text="dynamic button 3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

【讨论】:

  • 完美,线性布局的改变做到了!非常感谢!
【解决方案2】:

使用以下布局文件可能会解决您的问题。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:weightSum="1" >

    <TextView
        android:id="@+id/text_compliance_question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_gravity="center_horizontal"
        android:textSize="22sp"
        android:textStyle="bold"
        android:text="Hello world!" />

    <ImageView
        android:id="@+id/image_thumbnail"
        android:layout_weight="1"
        android:visibility="visible"
        android:layout_gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/device_type_windows" />

</LinearLayout>

当图像视图可见/消失时,它看起来像:

【讨论】:

  • 您好,非常感谢。这接近我想要的,但我如何得到它以便实际图像占用更多空间?例如,那个 Windows 徽标可能会增长到显示大小的 4 倍左右?谢谢!
  • 抱歉,我不太明白您的问题。您的意思是您的图像比 Windows 徽标大 4 倍还是比屏幕大?如答案所示,所有空间都可以用于图像,即蓝色矩形的高度和屏幕的宽度。
  • 看新图,把imageView的src的layout_width改成fill_parent就可以了。
猜你喜欢
  • 2015-03-03
  • 2020-06-13
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 2016-04-03
  • 2012-04-09
  • 1970-01-01
相关资源
最近更新 更多