如何在android中将布局平均分为两部分

如何在android中将布局平均分为两部分,android,view,Android,View,我希望在手机和平板电脑中,两个分隔器以两部分的形式平均布局。以下是我尝试过的事情: <LinearLayout 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"

我希望在手机和平板电脑中,两个分隔器以两部分的形式平均布局。以下是我尝试过的事情:

<LinearLayout 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=".LayoutActivity" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

您应该在布局中使用weightSum,并在两个文本视图中设置权重。请参见示例:

    <LinearLayout 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=".LayoutActivity" 
android:weightSum="1">

<TextView
    android:id="@+id/textview"
    android:layout_width="0dp"
    android:layout_weight="0.5"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textview1"
    android:layout_width="0dp"
    android:layout_weight="0.5"
    android:layout_height="wrap_content" />


您应该为其添加一个重量容器和一个方向,该方向取决于您想要垂直还是水平放置

      <LinearLayout 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"
    android:orientation="horizontal"
    tools:context=".LayoutActivity" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight= 0.5/>

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight= 0.5 />


使用线性布局的权重属性,使其成为您想要做的事情。就像你想平均分割文本视图,然后给出

android:layout_weight=“1”


在这两种文本视图中,在文本视图中使用权重。
android:weightSum
是可选的。您在LinearLayout中没有给定方向默认情况下,LinearLayout方向是水平的,因此由开发人员决定是水平的还是垂直的。如果他们想要垂直,则需要设置方向,因此您必须向新用户解释用户
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".LayoutActivity" 
    android:weightSum="1">

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_weight="0.5"
        android:layout_height="0dp" />

    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_weight="0.5"
        android:layout_height="0dp" />
</LinearLayout>