【问题标题】:Linear Layouts not obeying weighting不服从权重的线性布局
【发布时间】:2018-08-26 21:55:21
【问题描述】:

我有两个垂直线性布局,我想分别占据大约三分之一和三分之二的屏幕。我正在使用 layout_weight 来实现这一点,但它们都保持在 0dp 厚度而不是占用父级的可用宽度。

据我了解,当设置为 0dp 厚度时,这些布局应该占据 layout weighting / total weighting值得的重量。然而,这并没有发生。

这是我的 xml,第一个布局包含一个 ImageButton,第二个是空的:

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.3"
        android:layout_gravity="left"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="false"
            android:cropToPadding="false"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/silhouette" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.6"
        android:layout_gravity="right"
        android:orientation="vertical">


    </LinearLayout>

【问题讨论】:

  • 这两个线性布局的父级是什么?它应该是方向垂直的线性布局。子线性布局的宽度/高度也为 0,具体取决于首选方向
  • @Satya 所以他们必须在另一个垂直线性布局中才能工作?
  • 是的,请检查我的回答
  • 你能接受我的回答吗?

标签: android xml android-studio android-linearlayout


【解决方案1】:

试试这个布局(垂直方向)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="left"
    android:layout_weight="0.3"
    android:orientation="vertical">

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="false"
        android:cropToPadding="false"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/silhouette" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="right"
    android:layout_weight="0.6"
    android:orientation="vertical">


</LinearLayout>

水平方向

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="horizontal">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:layout_weight="0.3"
    android:orientation="vertical">

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="false"
        android:cropToPadding="false"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/silhouette" />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:layout_weight="0.6"
    android:orientation="vertical">


</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多