【问题标题】:How to divide screen space between ImageView and TextView in RelativeLayout如何在RelativeLayout中的ImageView和TextView之间划分屏幕空间
【发布时间】:2014-07-20 17:49:03
【问题描述】:

我需要输入ImageView,下面应该是textview。我通过使用RelativeLayout 中的文本和图像来完成此操作。一切正常,但我需要给ImageView 更多空间(2/3)。我怎样才能做到这一点 ? 提前谢谢。

<RelativeLayout
    android:id="@+id/rel_lay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_above="@+id/adPlaceholder" >
    <ImageView  
        android:id="@+id/image_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher"
        android:layout_centerHorizontal="true"  />

    <TextView
        android:id="@+id/text_main"
        android:layout_below="@+id/image_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"  
        android:text="@string/hello_world"/>
</RelativeLayout>

【问题讨论】:

  • 你可以在权重属性的相对布局中采用线性布局

标签: android android-layout android-relativelayout


【解决方案1】:

我觉得LinearLayout更适合你的情况 您可以将LinearLayout 与android:orientation="vertical" 和android:weightSum="3" 一起使用

然后您可以将android:layout_weight="2" 给ImageView 和android:layout_weight="1" 给TextView

更新后的最终XML代码

<LinearLayout
android:id="@+id/rel_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_above="@+id/adPlaceholder"
android:orientation="vertical"
android:weightSum="3" >

<ImageView  
    android:id="@+id/image_first"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:layout_weight="2"
    android:src="@drawable/ic_launcher"
    android:layout_centerHorizontal="true"  />

<TextView
    android:id="@+id/text_main"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:layout_centerHorizontal="true"  
    android:text="@string/hello_world"/>
</LinearLayout>

【讨论】:

    【解决方案2】:

    你想要什么,或者你的图片占用了更多的文字空间,你可以这样做:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/rel_lay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adPlaceholder"
        android:layout_alignParentLeft="true" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dip"
            android:gravity="center_horizontal"
            android:orientation="vertical" >
    
            <ImageView
                android:id="@+id/image_first"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher" />
    
            <TextView
                android:id="@+id/text_main"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="@string/hello_world" />
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      我认为最好的解决方案是放置一个LinearLayout inside ReletiveLayout 并将 weightsum 设置为内部 LinearLayout。

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/rel_lay"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_above="@+id/adPlaceholder"
          android:layout_alignParentLeft="true" >
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center_horizontal"
              android:orientation="vertical"
              android:weightSum="3" >
      
              <ImageView
                  android:id="@+id/image_first"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:layout_weight="1"
                  android:src="@drawable/ic_launcher" />
      
              <TextView
                  android:id="@+id/text_main"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:layout_weight="2"
                  android:text="@string/hello_world" />
          </LinearLayout>
      
      </RelativeLayout>
      

      还要注意layout_weight of ImageView is 1 out of 3 占父母的 2/3,TextView is 2 out of 3 占父母的 1/3。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-03
        • 1970-01-01
        • 1970-01-01
        • 2012-05-11
        • 2020-01-11
        • 1970-01-01
        相关资源
        最近更新 更多