2013-04-26 16 views
0

我有一個活動,可能有超過活動的水平寬度的信息,有沒有辦法添加一個水平滾動條,以便用戶可以根據需要向左和向右滾動?有沒有辦法在一個帶有tablelayout的佈局上添加水平滾動條?

我activity_fuel.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
<TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0"> 

    <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content">  

    <TextView 
     android:id="@+id/TextView01" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/cell_shape" 
     android:enabled="true" 
     android:scrollHorizontally="true" 
     android:text="@string/title_activity_address" 
     android:textStyle="bold" 
     android:width="100px" > 

    </TextView> 
    <TextView 
     android:id="@+id/textView02" 
     android:background="@drawable/cell_shape" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/title_activity_phone" 
     android:width="150px" 
     android:textStyle="bold"> 
    </TextView> 

    <TextView 
     android:id="@+id/TextView03" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/cell_shape" 
     android:scrollHorizontally="true" 
     android:text="@string/title_activity_distance" 
     android:textStyle="bold" 
     android:width="100px" > 
    </TextView> 
    </TableRow> 
</TableLayout> 


</RelativeLayout> 

我是相當新的Android開發,只是想有這些信息看起來有點入眼。

回答

2

1)你不應該使用像素(PX)設置你的View S的尺寸,而使用密度獨立像素(DP ),因爲它支持許多不同的屏幕尺寸/分辨率/像素密度更好。閱讀更多:http://developer.android.com/guide/practices/screens_support.html

2)關閉TextView這樣的標籤:<TextView ... />而不是<TextView ...></TextView>。它們都做同樣的事情,這使得一對標籤出來的東西不會在標籤之間託管任何東西似乎很奇怪...

3)佈局通常是沒用的,如果你只把一件東西放在它們裏面。在這裏你有一個RelativeLayout,在它只有一個TableLayout。您可以刪除RelativeLayout並將所有尺寸設置爲TableLayout

4)不要使用fill_parent,使用match_parent,他們基本上做同樣的事情,但fill_parent已經廢棄了,因爲API 8.如果你的目標更低的API級別,你很可能fill_parent,但絕對不要不使用兩者的混合物。 What is the difference between match_parent and fill_parent?

5)爲了得到你實際詢問的 - 你可以把你的TableLayout換成HorizontalScrollView。所以基本上用HorizontalScrollView代替RelativeLayout。應該做你想做的。

希望它有幫助。如果某件事不起作用或者你還有其他問題,不要害怕問。

+0

順便謝謝你的詳細答案,以及所有額外的信息。好東西。 – yams 2013-04-26 22:53:18

相關問題