簡體   English   中英

LinearLayout - 垂直不對齊

[英]LinearLayout - Vertically not align

我在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>

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>

最好將RelativeLayout用於這種類型的對齊,因為RelativeLayout的概念說它是“引用”一個控件

那么你的第二個線性布局是什么造成我猜你的麻煩,只需刪除它

您可以使用TableLayout而不是您的第二個LinearLayout.And在TableLayout中添加TableRow並在行中添加四個按鈕

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM