如何在android中将2个部分的布局平分

时间:2015-04-17 10:48:46

标签: 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"
    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>

4 个答案:

答案 0 :(得分:1)

您必须在布局中使用weightSum并在textview中设置权重,请参阅示例:

    <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" />

答案 1 :(得分:1)

您应该为它添加一个重量容器,并根据您的垂直或水平方向选择一个方向。

      <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 />

答案 2 :(得分:1)

使用线性布局的权重属性使其成为您想要做的。就像你想要平均分割textview然后给

  

机器人:layout_weight = “1”

中的

答案 3 :(得分:0)

     <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>
相关问题