【问题标题】:Dividing screen into two equal parts , can not set layout_weight将屏幕一分为二,不能设置layout_weight
【发布时间】:2016-02-01 23:01:27
【问题描述】:

我想这个问题很愚蠢,但我在这方面遗漏了一些东西并想了解。

我将屏幕分成两个相等的部分。我知道这可以通过设置权重来完成。但我不知道为什么我不能在线性布局中设置权重。相反,我正在设置 weightSum,但我对 weightSum 的理解是,我猜它是设置宽度和高度后屏幕上留下的空间。

这就是我正在做的事情

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="@+id/main_panel">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/top"
android:weightSum="100">
   <ListView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/list"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/top"
    android:weightSum="50"
    android:layout_alignParentBottom="true"
    android:id="@+id/ bottom">
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/data"
    android:orientation="vertical"
    android:background="#D3D3D3"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <View
        android:id="@+id/drag_line"
        android:layout_width="50dp"
        android:visibility="visible"
        android:background="@drawable/bar"
        android:layout_gravity="center_horizontal|top"
        android:layout_height="170dp"
        android:src="@drawable/line" />

</FrameLayout>
</LinearLayout>
</RelativeLayout>

PS:我无法使用 android lollipop 设置 layout_weight 属性

【问题讨论】:

  • 你想要哪两个部分?一边列表视图,另一边你想要什么?
  • 第一个带有列表视图的线性布局和第二个包含框架布局的线性布局
  • 在下面查看我的答案,它会起作用的。
  • 使用linearLayout代替Relative,你可以使用权重

标签: android android-layout android-linearlayout


【解决方案1】:

试试下面的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="@+id/main_panel">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/top"
>
   <ListView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/list"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_below="@id/top"
    android:layout_weight="1"
    android:layout_alignParentBottom="true"
    android:id="@+id/ bottom">
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/data"
    android:orientation="vertical"
    android:background="#D3D3D3"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <View
        android:id="@+id/drag_line"
        android:layout_width="50dp"
        android:visibility="visible"
        android:background="@drawable/bar"
        android:layout_gravity="center_horizontal|top"
        android:layout_height="170dp"
        android:src="@drawable/line" />

</FrameLayout>
</LinearLayout>
</RelativeLayout>

【讨论】:

  • 不,但为什么你给宽度 0dp,我需要两个垂直所以如果我将 orientation 设置为垂直,我不能将高度设置为 0dp
  • @user2323 你想要垂直还是水平?我是横向做的。请正确提及。
  • 对不起,我没有提到,我想要垂直但要使其垂直,我只需要设置方向并将高度设置为 0dp...我在这里正确吗?
【解决方案2】:

 <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
   
     <Button
        android:id="@+id/color_in_palette"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#CCFFFF"
        android:text="Button"
        android:layout_weight="50"
        android:textSize="0sp" />

   <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="50">  
    
     <TextView
         android:id="@+id/color_id"
         android:layout_width="match_parent"
         android:layout_height="50dp"
         android:text="(204,255,255)"
         android:gravity="center"
         android:textAppearance="?android:attr/textAppearanceLarge" />
          
    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:numColumns="13"
        android:layout_weight="48" >
    </GridView>

    </LinearLayout>
  

  </LinearLayout>

在这个按钮会占据屏幕的一半,而另一半被另一个LinearLayout占据,其中包含一个TextView & GridView

【讨论】:

    【解决方案3】:

    问题:

    您不能在相对布局中添加weightSum,它只绑定到线性布局布局参数本身。

    解决方案:

    在你的关系布局中添加另一个线性布局作为两个内部线性布局的父级。

    样本:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#ffffff"
    android:layout_height="match_parent"
    android:id="@+id/main_panel">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent"
    android:weightSum="1">
    
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/top"
    android:weightSum="0.5">
       <ListView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/list"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/top"
        android:weightSum="0.5"
        android:layout_alignParentBottom="true"
        android:id="@+id/ bottom">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/data"
        android:orientation="vertical"
        android:background="#D3D3D3"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
    
        <View
            android:id="@+id/drag_line"
            android:layout_width="50dp"
            android:visibility="visible"
            android:background="@drawable/bar"
            android:layout_gravity="center_horizontal|top"
            android:layout_height="170dp"
            android:src="@drawable/line" />
    
    </FrameLayout>
    </LinearLayout>
    
    </LinearLayout>
    </RelativeLayout>
    

    【讨论】:

    • 它没有分割,只有顶部可见
    【解决方案4】:

    Weightsum 是父级用来查找其子级的总重量和的属性。该属性仅适用于LinearLayout。它的孩子应该用宽度或高度指定它的重量为0dp。你可以找到教程here。所以你的 layout.xml 将是

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_panel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical"
        android:weightSum="100" >
    
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
    
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="50"
            android:divider="@null"
            android:dividerHeight="0dp" />
    
        <FrameLayout
            android:id="@+id/data"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="50"
            android:background="#D3D3D3"
            android:orientation="vertical" >
    
            <View
                android:id="@+id/drag_line"
                android:layout_width="50dp"
                android:layout_height="170dp"
                android:layout_gravity="center_horizontal|top"
                android:visibility="visible" />
        </FrameLayout>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案5】:
      Get Root attribute always be <Relative> and with in that write your remaining XML code.
      
      e.g.
      
      <?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"
        >
      
      
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1"
              android:background="@color/black">
      
          </LinearLayout>
      
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1"
              android:background="@color/white">
      
          </LinearLayout>
      
      
      </LinearLayout>
      
      //Replace above XML code by...
      
      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"
                >
      
      
      
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="0dp"
                          android:layout_weight="1"
                          android:background="@color/black">
      
                      </LinearLayout>
      
      
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="0dp"
                          android:layout_weight="1"
                          android:background="@color/white">
      
                      </LinearLayout>
      
      
                  </LinearLayout>
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多