Android 百分比布局高度

新手上路,请多包涵

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

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

  </LinearLayout>
</LinearLayout>

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

做这个的最好方式是什么?

原文由 Somk 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 923
2 个回答

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

原文由 fleetway76 发布,翻译遵循 CC BY-SA 3.0 许可协议

通过引入 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>

结果视图

您可以在这篇 使用 ConstraintLayout 构建接口的 文章中阅读更多内容。

原文由 Eugene Brusov 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题