Android 安卓:布局重量不适用于按钮和线性布局
我试图让所有的按钮大小相同,并且都在屏幕的右侧,无论文本视图中有多少文本 现在,按钮位于垂直线性布局(由两个文本视图组成)的旁边,并且仅当其中一个文本视图超过一行时才位于正确位置 我曾尝试将按钮添加到LinearLayout并赋予其权重,为每个文本视图赋予权重,仅为填充文本的LinearLayout赋予权重,等等,但都没有效果 我现在想知道这是否是一个问题,因为这不是主XML,而是一个重复项?但我认为这不会影响它 这是行项目的XML:Android 安卓:布局重量不适用于按钮和线性布局,android,xml,android-layout,android-layout-weight,Android,Xml,Android Layout,Android Layout Weight,我试图让所有的按钮大小相同,并且都在屏幕的右侧,无论文本视图中有多少文本 现在,按钮位于垂直线性布局(由两个文本视图组成)的旁边,并且仅当其中一个文本视图超过一行时才位于正确位置 我曾尝试将按钮添加到LinearLayout并赋予其权重,为每个文本视图赋予权重,仅为填充文本的LinearLayout赋予权重,等等,但都没有效果 我现在想知道这是否是一个问题,因为这不是主XML,而是一个重复项?但我认为这不会影响它 这是行项目的XML: <LinearLayout xmlns:androi
<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_alignParentEnd="true"
您可以添加
Margin
属性,为视图提供一些额外的空间。您可以使用水平方向的线性布局,其中包含两个视图:
- 您的文本容器(可能是带有两个文本视图的垂直线性布局)制作线性布局宽度0dp和重量:1
- 您的按钮(可能有一些边距和固定宽度)
<?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>
所以基本上你会在你的按钮上看到这样的东西使用android:layout\u weight=“0”。你能用problem@Psypher在问题中,我添加了一个链接,指向当前的屏幕截图。我看到我的问题被否决了,如果问得不好,我道歉。谢谢大家的回答,我很快会尝试一些,但是它们都是解决问题的不同方法,有人知道我的想法到底有什么问题吗?谢谢,我理解这个想法。然而,我完全复制了这段代码和想法,虽然我理解它,但我得到了完全相同的问题,按钮仍然不在边缘。我看到您已经接受了@Barns解决方案。然而,我编辑了我的答案,所以如果有人想坚持线性布局,他们可以尝试一下。对于前面的代码我很抱歉,它有一些小的语法错误(正如我提到的,我还没有测试它)。我决定直接使用第一个布局作为空间填充布局,因为如果文本大于一行,则空间视图可能会出现问题。这不起作用,而是在屏幕左侧的空间中有3个按钮,每个按钮占据屏幕的1/3,并且没有文本可见。不确定如何使用3个按钮结束。。。这应该会在屏幕的右侧添加一个按钮,每行在左侧添加两个文本视图。对不起,我的意思是屏幕上总共有3个按钮,它们每一个都在一行中,该行占据屏幕的1/3,并且按钮都在左侧。我不知道为什么它不工作,当它似乎应该好了,好吧,看起来你有一些工作与RelativeLayout,所以祝你好运。我只是查了一下我的一个版本,我把布局高度搞错了,应该是包装内容(更新答案),这可能是搞乱它的原因。啊,好的,谢谢你的回答和帮助。如果可以的话,我会投票给你答案,但是我没有足够的声誉。非常感谢你,这是唯一正确的回答。你能顺便指出一下我最初的方法到底出了什么问题吗?我是Android布局新手,不想在游戏中犯类似的错误future@Zuki当前位置您与原始布局的距离确实不太远,但是
weightSum
和layout\u weight
一直都很难处理--而且需要进行大量的尝试和错误才能使其正确。在Android开发的早期,layout\u weight
是获得“相对”定位的唯一方法。幸运的是,谷歌添加了许多新的布局
方案,这使得获得你想要的设计变得更加容易——更加容易。如果我没有错的话,这将使两个视图(LLayout和Button)占据50%的宽度,因为它们都具有相同的权重,并且它们的父视图的权重总和为2。然而,这是一个好主意。我唯一的解决办法是将按钮宽度设置为固定宽度或环绕宽度。
<?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>