【问题标题】:Android Percentage Layout HeightAndroid 百分比布局高度
【发布时间】:2011-09-06 09:53:34
【问题描述】:

我知道设置百分比是不可能的,您可以设置某些图像的权重来缩放它们的高度。我正在尝试做的是指定布局相对于其所在布局的高度。基本上我有这样的东西

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

  </LinearLayout>
</LinearLayout>

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

最好的方法是什么?

【问题讨论】:

标签: android xml layout


【解决方案1】:

有一个属性叫做android:weightSum。

您可以在父 linear_layout 中设置 android:weightSum="2",在内部 linear_layout 中设置 android:weight="1"。

记得将内部的 linear_layout 设置为 fill_parent 以便 weight 属性可以按预期工作。

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

<LinearLayout
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:weightSum="2">

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

</LinearLayout>

【讨论】:

  • 仅供参考,如果您使用重量,最好将高度(或宽度取决于您的使用情况)设置为 0dp 以获得更好的性能
  • 完全同意@Gerard。 weight 仅在我将高度设置为 0dp 时才有效
  • 对我来说,这仅在我将 android:orientation="vertical" 添加到父 LinearLayout 时才有效。
  • 我们如何以编程方式进行? @AxelM.Garcia
  • 请根据@Gerard 和 Baby 的 cmets 修改答案,因为这仅适用于 android:layout_height="0dp" 至少在某些情况下(我的所有情况都是如此)。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 完美运行,我希望它能在不同尺寸上自动缩放。
    • @89n3ur0n 是的,它是设备尺寸的百分比。
    【解决方案4】:

    正如你所说,我建议使用权重。百分比会非常有用(不知道为什么不支持它们),但是您可以这样做的一种方法是:

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

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

    【讨论】:

      【解决方案5】:

      引入了ContraintLayout,可以用Guidelines来实现:

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

      您可以在这篇文章中阅读更多内容Building interfaces with ConstraintLayout

      【讨论】:

        猜你喜欢
        • 2013-05-19
        • 2012-08-31
        • 2012-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-06
        • 2021-10-30
        • 1970-01-01
        相关资源
        最近更新 更多