2017-09-02 76 views
0

我有一个简单的LinearLayout,我试着让它看起来像这样: look2TextView的重力下不工作

相反,它是这样的: look1

我的XML是:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="miasto" 
      android:textSize="10sp"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="ulica" 
      android:textSize="15sp"   
      android:gravity="bottom" 
      /> 
    </LinearLayout> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:textSize="30sp" 
     android:text="22" 
     android:gravity="bottom"/> 

</LinearLayout> 

为什么gravity =“bottom”不起作用?

+0

答案就在这里: https://stackoverflow.com/questions/4768738/android-textview-remove-spacing-and-padding-on-top-and-bottom –

回答

1

尝试使您的textview高度match_parent。

0

它添加到textViews

android:textAlignment="gravity" 
0

你设置这个文本查看wrap_content宽度这意味着,以往的文字是什么,认为采取文字和LinearLayout的大小,默认重力center

因此,更改

android:layout_height="wrap_content" 

android:layout_height="match_parent" 

同时,仅供参考,gravity是文本在TextView中排列的方式。 TextViewwrap_content中不做任何事情,因为TextView与文本的大小完全相同。

layout_gravityTextView将调整本身在其父的方式,在LinearLayout你的情况。

+0

我缺少的东西?右侧的TextView具有match_parent高度。我知道布局重力和重力之间的区别。如果text_size相同 - 两个底部文本视图都是同一行,但如果不是这样的话...... –

+0

那是因为'LinearLayout'有'android:layout_height =“wrap_content”'。将其更改为'match_parent'并查看它是否有效。 – Lal

+0

我对Android有所了解,并花了几个小时试图理解这一点。当然,我已经测试了很多配置。他们都没有工作。如果你有一些请粘贴工作代码。 –

0

如果文本大小并不重要,只要改变文字大小:

<TextView 
 
     android:id="@+id/textView4" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="match_parent" 
 
     android:gravity="bottom" 
 
     android:text="22" 
 
     android:textSize="18sp"/>

我测试这和它的确定。

+0

文字大小在这里很重要。作为一种解决方法,我只在左下方的textview中使用填充。内部字体填充是这里的问题。检查我的问题评论中添加的链接。 –