Android: layout_weight 无法使用 Button 和 LinearLayout

Android: layout_weight not working with Button and LinearLayout

我试图让所有按钮都具有相同的大小,并且都在屏幕的右侧,无论文本视图中有多少文本。

现在,按钮位于垂直 LinearLayout(由 2 个 TextView 组成)旁边,并且仅当其中一个 TextView 超过一行时才会位于正确的位置。

我试过将 Button 添加到 LinearLayout 并为其赋予权重,为每个 TextView 赋予权重,仅为填充文本的 LinearLayout 赋予权重,等等都无济于事。

我现在想知道这是否是一个问题,因为这不是主要的 XML,而是一个重复的项目?不过没想到会影响到。

This is what it currently looks like

这是行项目的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<LinearLayout
    android:id="@+id/linearlayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Header" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

这就是您的 xml 应该满足您的需求

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2">

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Header" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

您也可以使用 ConstraintLayout

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Header"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Second text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/header"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

您可以使用多种不同的布局来实现您想要的设计。您可以通过将外部布局元素从 LinearLayout 更改为 RelativeLayout

来使用这种方法
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@+id/button"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Header" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:text="Button" />
</RelativeLayout>

注意你只需要添加属性:

android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/button"

到内部 LinearLayout 并将此属性添加到 Button 视图:

android:layout_alignParentEnd="true"

您可以添加 Margin 属性给视图一些额外的空间。

您可以只使用包含 2 个视图的水平方向的 LinearLayout :

  • 您的文本容器(可能是带有两个 TextView 的垂直 LinearLayout)。 使 LinearLayout 宽度为 0dp 并且 weight:1
  • 您的按钮(可能有一些侧边距和固定宽度)

所以实际上第一个 LinearLayout 将填充所有可用的水平 space 因此将按钮推到右边缘。

一个小例子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_gravity="center_vertical"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Header" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Some random text" />

    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:text="Button" />

</LinearLayout>

所以基本上你会得到这样的东西