如何使用两个相等的LinearLayouts拆分屏幕?

Eug*_*ene 57 android android-linearlayout

想用两个LinearLayouts为我的应用程序拆分屏幕.我应该使用哪些参数来精确分割两个相等的部分 - 第一个是LinearLayout,第二个是在它下面.

Kon*_*rov 122

使用权重参数,粗略布局将如下所示:

<LinearLayout android:orientation="horizontal"
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent">

    <LinearLayout 
        android:layout_weight="1" 
        android:layout_height="fill_parent" 
        android:layout_width="0dp"/>

    <LinearLayout 
        android:layout_weight="1" 
        android:layout_height="fill_parent" 
        android:layout_width="0dp"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • @Marek据我记得,只要他们是平等的,这并不重要. (7认同)
  • 看看有关使用layout_weight属性的本教程http://www.chess-ix.com/2012/01/17/the-use-of-layout_weight-with-android-layouts/ (3认同)

sil*_*war 43

我在4 - 5年后回答了这个问题,但最佳实践如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">

   <LinearLayout
      android:id="@+id/firstLayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_toLeftOf="@+id/secondView"
      android:orientation="vertical"></LinearLayout>

   <View
      android:id="@+id/secondView"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_centerHorizontal="true" />

   <LinearLayout
      android:id="@+id/thirdLayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_toRightOf="@+id/secondView"
      android:orientation="vertical"></LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是正确的方法,因为对于UI操作,layout_weight的使用总是很重要.使用LinearLayout同等拆分布局并不是一种好习惯

  • @Woppi - 检查这个要点,让我知道这是否能回答你的疑问.https://gist.github.com/silwar/ba2679baf5d5681796228e2fe9d01645 (4认同)

sta*_*bit 15

把它放在那里:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000"
    android:weightSum="4"
    android:padding="5dp"> <!-- to show what the parent is -->
    <LinearLayout
        android:background="#0000FF"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="2" />
    <LinearLayout
        android:background="#00FF00"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


Nje*_*rus 6

为了将ui分为两个相等的部分,您可以在父级LinearLayout中使用weightSum为2,并为每一个分配layout_weight,如下所示

<?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"
    android:weightSum="2">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

        </LinearLayout>

       <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

       </LinearLayout>


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)