线性布局不对齐基线,相对布局不支持权重。

4

我有一个纵向排列的线性布局,其中包含多个视图组。其中一个是:

<LinearLayout
   android:id="@+id/addedit_updowncontrol"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_below="@+id/textView2"
   android:layout_marginBottom="5dp" 
   android:orientation="horizontal">

    <Button
        android:id="@+id/addedit_btndecrement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_weight="0.1"
        android:text="-" />

    <EditText
        android:id="@+id/addedit_txtQuantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="90dp"
        android:layout_weight="0.4"
        android:layout_alignParentTop="true"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/addedit_btnIncrement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_weight="0.1"
        android:layout_toLeftOf="@+id/addedit_units"
        android:text="+" />

    <Spinner
        android:id="@+id/addedit_units"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:layout_weight="0.4"
        android:layout_alignParentTop="true"
    android:layout_alignParentRight="true" />

</LinearLayout>

这个问题的表现类似于 这个 (抱歉只能提供链接,希望有更好的方法)。这是不可接受的,因为按钮与编辑文本框不对齐。
我尝试切换到RelativeLayout,得到了像这个的结果。这也是不可接受的,因为这两个按钮(- 和 +)的宽度不同。
我想实现的目标是: (1) 按钮的基线与该行中的其他控件对齐。 (2) 两个按钮具有相同的宽度。
我似乎可以使用RelativeLayout实现(1),并使用LinearLayout实现(2)。但两者不能兼得。
请问有谁能帮助我吗?
5个回答

8

使用

android:gravity="center_vertical"

在你的LinearLayout中,所有的项目将会居中显示。


谢谢!这个方法可行,看起来比设置android:layout_marginTop="-3dp"更好。 - Code Poet
我确信这是布局管理器或负责布局的人的一个错误。 - Code Poet

1

在xml中将线性布局的方向更改为水平,并将baselinealigned属性设置为true。尝试使用layout_weights进行布局。将相同的权重分配给两个按钮。希望这可以帮助到您。


2
基准线对齐默认为打开状态。 - Romain Guy

0
你可以尝试几件事情。首先将你的LinearLayout方向设置为水平。你尝试将RelativeLayout放在LinearLayout里面了吗?也许这个方法也行:将按钮设置为wrap_content,将edit_text设置为fill_parent?

只需将您的LinearLayout的方向设置为水平即可。我试过了,对我有效。 - nostradamus
还可以尝试将EditText的marginTop设置得更高一些。android:layout_marginTop(5dip)或其他可能有效的整数值。 - nostradamus
将LinearLayout设置为水平方向对我没有起作用。我仍然有对齐问题。我尝试使用负的android:layout_marginTop,如下所示:android:layout_marginTop="-3dp"。这确实使按钮与EditText对齐。但是,我不确定这是否会在所有设备上都有效。 - Code Poet

0

我知道这不是很好,但保留LinearLayout并在EditText和Spinner上添加paddingTop="2dp"或marginTop="2dp"。

这些元素的9patch资源包括底部填充,使它们看起来与其他所有元素不协调。

(我认为是2dp,可能是3dp)

不确定EditText在3.0+上是否仍存在此问题,因为holo主题中EditText的默认设计非常不同。


0

你为LinearLayout指定了很多无用的属性。例如,你不需要所有这些layout_alignParentToplayout_toLeftOf用于LinearLayout,它们是在RelativeLayout中使用的。试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   android:id="@+id/addedit_updowncontrol"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginBottom="5dp" 
   android:layout_below="@+id/textView2"
   android:orientation="horizontal" >

    <Button
        android:id="@+id/addedit_btndecrement"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:text="-" />

    <EditText
        android:id="@+id/addedit_txtQuantity"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:minWidth="90dp"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/addedit_btnIncrement"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:text="+" />

    <Spinner
        android:id="@+id/addedit_units"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center" />

</LinearLayout>

我知道,对此我感到抱歉。我一直在切换布局,看看能否找到一个可行的。据我所知,那些不相关的布局会被忽略掉。但我会尝试清理一下,看看是否有所改善。 - Code Poet
我已经为你完成了这个任务。我还修复了你的布局,请试一下。 - a.ch.
谢谢。我明白了。基本上设置android:gravity =“center_vertical”似乎可以解决问题。 - Code Poet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接