2011-03-08 23 views
2

一直在試圖找出Android佈局和有點問題。我得到了儘可能找到我想要的地方的對象,使用RelativeLayout(嘗試LinearLayout和TableLayout組合,只發現重力和屬性沒有做我認爲他們會做的事情...)但我我們遇到了一個與其他文本背後的文字出血有關的問題。RelativeLayout保證金問題,文字滲入其他「字段」

Valid XHTML

第二項說明了問題。我想有沒有文字出血的相同佈局。這裏是我的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="?android:attr/listPreferredItemHeight" 
     android:layout_width="wrap_content" 
     android:padding="6dip"> 
    <TextView android:id="@+id/item_text_product" 
      android:layout_alignParentLeft="true" 
      android:textSize="24sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
    <TextView android:id="@+id/item_text_total" 
      android:layout_alignParentRight="true" 
      android:textSize="18sp" 
      android:textStyle="bold" 
      android:textColor="#00aa00" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
    <TextView android:id="@+id/item_text_count" 
      android:layout_alignParentRight="true" 
      android:layout_below="@id/item_text_total" 
      android:textSize="12sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
    <TextView android:id="@+id/text_count" 
      android:text="Count: " 
      android:textSize="12sp" 
      android:layout_toLeftOf="@id/item_text_count" 
      android:layout_alignBaseline="@id/item_text_count" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
</RelativeLayout> 

回答

1

在產品TextView上設置maxEms屬性,使其寬度永遠不會超過n ems。

這種方式應該是最好的解決方案。

+0

這是最快,最簡單的解決方案。像冠軍一樣工作。謝謝。 – moonlightcheese 2011-03-08 20:56:31

+1

似乎不是處理不同屏幕尺寸的好方法... – 2011-03-08 21:33:22

0

如果你不想重疊行爲,你可以嘗試使用LinearLayout。

是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:layout_width="wrap_content" 
    android:padding="6dip"> 
    <TextView android:id="@+id/item_text_product" 
     android:textSize="24sp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <TextView android:id="@+id/item_text_total" 
      android:layout_alignParentRight="true" 
      android:textSize="18sp" 
      android:textStyle="bold" 
      android:textColor="#00aa00" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <TextView android:id="@+id/item_text_count" 
      android:layout_alignParentRight="true" 
      android:layout_below="@id/item_text_total" 
      android:textSize="12sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <TextView android:id="@+id/text_count" 
      android:text="Count: " 
      android:textSize="12sp" 
      android:layout_toLeftOf="@id/item_text_count" 
      android:layout_alignBaseline="@id/item_text_count" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
    </RelativeLayout> 
</LinearLayout> 

layout_weight =「1」將導致該視圖來擴展以填充未由右手視圖佔據的可用空間。

0

問題與wrap_content一起,視圖將擴展以適應其內容。

如何縮小文本並使用嵌套的線性佈局?

1

我想你只需要將android:layout_toRightOf="@id/item_text_product"添加到第二個textView,但現在不能嘗試。

2

首先把你的item_text_counttext_count項目(我的意思是,把它們放在XML的text_count以上)...然後:

<TextView android:id="@+id/item_text_total" 
     android:layout_alignParentRight="true" 
     android:layout_toLeftOf="@id/text_count" 
     android:textSize="18sp" 
     android:textStyle="bold" 
     android:textColor="#00aa00" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

正如你看到的,所不同的是android:layout_toLeftOf="@id/text_count"