的LinearLayout - 垂直不齐 [英] LinearLayout - Vertically not align

查看:262
本文介绍了的LinearLayout - 垂直不齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题的LinearLayout在Android上。我有四个按钮。每个按钮都有一个固定的大小,但文本的长度可以变化。

我的问题是,它们不与每个的顶部对齐。他们看到与变化取决于数量每个钮内的文本的顶部对齐线有内按钮(见图片)。

另外,我想用的LinearLayout,我最终将使用code将根据数据库中的数据添加按钮来保存。

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:方向=垂直
  机器人:layout_width =match_parent
  机器人:layout_height =match_parent>
       <的LinearLayout的android:layout_width =match_parent机器人:layout_height =match_parent>
           <按钮的android:文本=1号线2号线机器人:TEXTSIZE =30dp的android:layout_width =160dp机器人:layout_height =120dp>< /按钮>
           <按钮的android:文本=1号线2号线3号线机器人:TEXTSIZE =30dp的android:layout_width =160dp机器人:layout_height =120dp>< /按钮>
           <按钮的android:文本=1号线机器人:TEXTSIZE =30dp的android:layout_width =160dp机器人:layout_height =120dp>< /按钮>
           <按钮的android:文本=1号线机器人:TEXTSIZE =30dp的android:layout_width =160dp机器人:layout_height =120dp>< /按钮>
       < / LinearLayout中>< / LinearLayout中>

修改:ANSWER(不能回答我的问题):

好吧,我只是我自己找到了答案。
你必须添加的android:baselineAligned =假来的LinearLayout或其他任何类似的控制,可以显示相同的行为。

您可以在UI设计器使用名为切换基线对齐按钮,还解决这个问题。

所以,最终code是:

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:方向=垂直
  机器人:layout_width =match_parent
  机器人:layout_height =match_parent>
       <的LinearLayout机器人:baselineAligned =false的机器人:layout_width =match_parent机器人:layout_height =match_parent>
           <按钮的android:文本=1号线2号线机器人:TEXTSIZE =30dp的android:layout_width =160dp机器人:layout_height =120dp>< /按钮>
           <按钮的android:文本=1号线2号线3号线机器人:TEXTSIZE =30dp的android:layout_width =160dp机器人:layout_height =120dp>< /按钮>
           <按钮的android:文本=1号线机器人:TEXTSIZE =30dp的android:layout_width =160dp机器人:layout_height =120dp>< /按钮>
           <按钮的android:文本=1号线机器人:TEXTSIZE =30dp的android:layout_width =160dp机器人:layout_height =120dp>< /按钮>
       < / LinearLayout中>< / LinearLayout中>


解决方案

它能够更好地使用RelativeLayout的这种对齐类型,如RelativeLayout的的概念说,它控制的引用

I got a problem with LinearLayout on Android. I have four buttons. Each button has a fixed size, but the text can vary in length.

My problem is that they are not align with the top of each. They seen to be align with the top of the text inside of each botton which change depending on the number of line there is inside the button (See picture).

Also, I want to keep using LinearLayout as I will eventually use code will add buttons based on data from a database.

<?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>

EDIT: ANSWER (Can't answer my own question):

Ok, I just found the answer by myself. You have to add android:baselineAligned="false" to LinearLayout or any other similar control that could show the same behavior.

You can also fix this in the UI designer using the button called "Toggle Baseline Alignment".

So the resulting code is:

<?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>

解决方案

its better to use RelativeLayout for such type of alignment, as RelativeLayout's concept says that it "reference" of a control

这篇关于的LinearLayout - 垂直不齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆