Android百分比布局高度

Android百分比布局高度,android,xml,layout,Android,Xml,Layout,我知道不可能设定百分比,你可以设定某些图像的权重来衡量它们的高度。但我想做的是指定布局相对于其所在布局的高度。基本上我有这样的东西 <LinearLayout android:layout_height="fill_parent"> <LinearLayout> </LinearLayout> </LinearLayout> 当然,这是一个非常简化的版本,只是为了让你能理解我的胡言乱语。基本上,我希望将内线布局设置为主线布局的50%

我知道不可能设定百分比,你可以设定某些图像的权重来衡量它们的高度。但我想做的是指定布局相对于其所在布局的高度。基本上我有这样的东西

<LinearLayout android:layout_height="fill_parent">
  <LinearLayout>

  </LinearLayout>
</LinearLayout>

当然,这是一个非常简化的版本,只是为了让你能理解我的胡言乱语。基本上,我希望将内线布局设置为主线布局的50%左右


最好的方法是什么?

您可以在该布局下方添加另一个空布局,并将它们设置为具有相同的布局权重。他们每人应该得到50%的空间。

正如你所说的,我推荐重量。百分比会非常有用(不知道为什么它们不受支持),但有一种方法可以做到这一点:

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


外卖是你有一个空的视图,这将占用剩余的空间。不理想,但它能满足您的需求。

有一个名为android:weightSum的属性

您可以在父线性布局中设置android:weightSum=“2”,在内部线性布局中设置android:weight=“1”

记住将内部线性布局设置为填充父对象,以便权重属性可以按预期工作

顺便说一句,我不认为有必要添加第二个视图,尽管我没有尝试。:)


android:layout_weight=“.YOURVALUE”是按百分比实现的最佳方式


随着ContrainLayout的引入,可以使用以下指南实施:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.eugene.test1.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#AAA"
        android:text="TextView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/guideline" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

</android.support.constraint.ConstraintLayout>


您可以在本文中阅读更多内容。

FYI,如果您使用重量,最好将高度(或宽度,取决于您的使用情况)设置为0dp,以获得更好的性能,并与@Gerard一致。仅当我将身高设置为
0dp
时,体重才起作用。仅当我将
android:orientation=“vertical”
添加到父级
线性布局时,体重才起作用。我们如何通过编程实现@AxelM.garcia请根据@Gerard and Baby的评论修改答案,因为这仅在android:layout_height=“0dp”至少在某些情况下有效(这对我的所有案例都适用)。使用支持库,您现在可以使用PercentRelativeLayout和PercentFrameLayout:完美工作,我希望它能在不同的尺寸上自动伸缩。@89n3ur0n是的,它是设备尺寸的%。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/logTextBox"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".20"
        android:maxLines="500"
        android:scrollbars="vertical"
        android:singleLine="false"
        android:text="@string/logText" >
    </TextView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.eugene.test1.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#AAA"
        android:text="TextView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/guideline" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

</android.support.constraint.ConstraintLayout>