首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓: layout_weight不支持按钮和LinearLayout

安卓: layout_weight不支持按钮和LinearLayout
EN

Stack Overflow用户
提问于 2018-06-13 06:05:05
回答 3查看 1.1K关注 0票数 1

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

现在,按钮紧挨着垂直的LinearLayout (由2个TextViews组成),并且只有当其中一个TextViews超过一行时才会出现在正确的位置。

我尝试过将按钮添加到LinearLayout并给它一个权重,给每个TextViews一个权重,只给文本填充的LinearLayout一个权重,等等都没有用。

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

This is what it currently looks like

这是行项的XML:

代码语言:javascript
复制
<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" />

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-13 06:45:26

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

代码语言:javascript
复制
<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>

请注意,您只需添加以下属性:

代码语言:javascript
复制
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/button"

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

代码语言:javascript
复制
android:layout_alignParentEnd="true"

您可以添加Margin属性来为视图提供一些额外的空间。

票数 0
EN

Stack Overflow用户

发布于 2018-06-13 06:11:46

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

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2018-06-13 06:29:50

您也可以使用ConstraintLayout来实现这一点

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50826487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档