【问题标题】:how to have 2 relative layouts divided by half in activity?如何在活动中将 2 个相对布局除以一半?
【发布时间】:2016-09-12 12:17:34
【问题描述】:

我想把我的屏幕垂直分成两半,每一半做不同的颜色,

我试图使用这里提到的这个解决方案>

Android: 2 relative layout divided in half screen

但它对我不起作用。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activities.alignmentActivities.Add_New_Project_Activity"
    tools:showIn="@layout/app_bar_add__new__project_">


        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#f00000"
            android:layout_weight="1">
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:background="#00b0f0"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        </RelativeLayout>

</LinearLayout>

我怎样才能达到 2 个布局的预期效果,每个布局占屏幕垂直的一半\我做错了什么然后链接中提到的示例?

【问题讨论】:

  • android:layout_height="wrap_content" 更改为 match_parent
  • 要垂直分屏吗?
  • “但它对我不起作用”是什么意思?你具体做了什么?发生了什么?

标签: android android-studio layout


【解决方案1】:

你可以试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2"
    android:orientation="horizontal"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="#f00000"
        android:layout_weight="1">
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:background="#00b0f0"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </RelativeLayout>

输出:

【讨论】:

    【解决方案2】:

    在您的 LinearLayout 中添加以下属性

     android:weightSum="2"
    

    添加此代码后,您的代码将起作用。 WeightSum 属性定义了你希望它被划分的等分的总数。如果你希望它被划分为 3 部分,请将其值更改为 3。

    【讨论】:

      【解决方案3】:
      <?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="horizontal">
      
             <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/red">
             </RelativeLayout>
      
             <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/white">
             </RelativeLayout>
      </LinearLayout>
      
      **Preview Image Link**
      
        [1]: http://i.stack.imgur.com/6JKTP.png
      

      【讨论】:

        【解决方案4】:

        如果你想垂直分割屏幕而不是你需要使用android:orientation="vertical",在子布局中使用android:layout_width="match_parent"

        检查下面xml

         <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false"
            android:orientation="vertical"
            android:weightSum="2"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" >
        
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#f00000" >
            </RelativeLayout >
        
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#00b0f0" >
            </RelativeLayout >
        </LinearLayout >
        

        【讨论】:

          猜你喜欢
          • 2013-11-27
          • 2013-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-22
          • 1970-01-01
          • 2016-12-15
          相关资源
          最近更新 更多