2013-04-25 46 views
3

我想在我的android應用程序的ListView中的項目包含左邊的複選框,其次是一些文本,後面跟着一個按鈕。我希望複選框和按鈕與文本基線對齊。爲了達到這個目的,我把所有這三個放在一個LinearLayout中,默認情況下它會嘗試基線對齊它的項目。這樣做效果很好,除了多行文本的底線總是被剪切。基線對齊的TextView剪輯多行文本的底線

這裏是它的外觀的例子:

Example of the problem

有StackOverflow上一個related question提出的方案是設置layout_gravity =「補」爲TextView的,但失敗的目的因爲它禁用了基線對齊。有沒有解決這個問題的方法,可以讓我保留基線對齊?這似乎是很多人必須遇到的一個非常基本的問題。

我發現的唯一的其他解決方案是在文本視圖的底部添加一些填充,但對於字體大小而言,這看起來很不起作用且不夠健壯。我可以從基於字體大小的代碼中程序化地設置填充,但似乎要獲得正確的值可能會非常棘手。

下面是一個示例佈局示出了此問題:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingBottom="8dp" 
    android:paddingTop="8dp" 
    android:baselineAligned="true"> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="9dp" 
     android:layout_marginRight="8dp" 
     android:textSize="16sp"/> 

    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Woah, this is a long text line, enough that tit has to wrap to multiple lines, thus demonstrating our problem. In fact, it wraps to multiple lines! So many lines! I hope they all show up correctly!" 
      android:textSize="16sp"/> 

    <FrameLayout 
     android:layout_width="40dp" 
     android:layout_height="match_parent"> 

     <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="12dp" 
       android:src="@drawable/ic_note_check_delete"/> 
    </FrameLayout> 
</LinearLayout> 
+0

變化的TextView高度'fill_parent'主意? – StoneBird 2013-04-26 00:11:29

+0

感謝您的建議,但這不起作用。它實際上使得文本變得更小並且剪輯更多,因爲「fill_parent」有點用詞不當,所以他們爲什麼稍後將其改變爲「match_parent」。 – alvion 2013-04-26 17:25:12

+0

如果在線性佈局中將'android:layout_height'更改爲'fill_parent'會有幫助嗎?它解決了我的機器上的剪輯問題,但是這種改變會滿足您的項目需求還是會引發新的問題? – StoneBird 2013-04-26 20:31:38

回答

0

我終於通過改變TextView的佈局重力center_vertical解決類似的問題。

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" /> 

https://stackoverflow.com/a/11246243/3681880