LinearLayout - 垂直不对齐

dar*_*gel 17 android button android-linearlayout

我在Android上遇到了LinearLayout的问题.我有四个按钮.每个按钮都有固定的大小,但文本的长度可能不同.

我的问题是它们不与每个顶部对齐.它们看起来与每个按钮内部文本的顶部对齐,这取决于按钮内部的行数(参见图片).

此外,我想继续使用LinearLayout,因为我最终将使用代码将基于数据库中的数据添加按钮.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
       <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
           <Button android:text="Line1 Line2" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1 Line2 Line3" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
       </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

http://i.imgur.com/0Kj8h.png

编辑:答案(不能回答我自己的问题):

好的,我自己就找到了答案.您必须将android:baselineAligned ="false"添加到LinearLayout或任何其他可能显示相同行为的类似控件.

您还可以使用名为"Toggle Baseline Alignment"的按钮在UI设计器中修复此问题.

所以生成的代码是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
       <LinearLayout android:baselineAligned="false" android:layout_width="match_parent" android:layout_height="match_parent">
           <Button android:text="Line1 Line2" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1 Line2 Line3" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
           <Button android:text="Line1" android:textSize="30dp" android:layout_width="160dp" android:layout_height="120dp"></Button>
       </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

the*_*abh 0

好吧,我猜你的第二个线性布局给你带来了麻烦,只需删除它