2011-04-13 31 views
6

我不想使用框架提供的首選項,而是我想創建一個看起來類似的ListView。特別是,我希望它爲TextView使用相同的字體大小和樣式。Android:如何製作一個類似於首選項的ListView?

+0

這完全取決於你在說什麼框架。每個供應商都有特定的調整來區別於其他供應商。您可以隨時從您的fw中獲取Settings.apk,使用apk管理器反編譯它,然後深入xml以獲取想法。 – Aleadam 2011-04-13 21:58:06

回答

9

這不是ListView控件本身,而是ListView控件中顯示的子視圖。它們是由適配器的getView方法創建的。

要創建類似於Android的視圖,您可以使用Android源代碼,特別是相關的XML文件佈局。例如,preference.xml看起來是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:gravity="center_vertical" 
    android:paddingRight="?android:attr/scrollbarSize"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dip" 
     android:layout_marginRight="6dip" 
     android:layout_marginTop="6dip" 
     android:layout_marginBottom="6dip" 
     android:layout_weight="1"> 

     <TextView android:id="@+android:id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" /> 

     <TextView android:id="@+android:id/summary" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@android:id/title" 
      android:layout_alignLeft="@android:id/title" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:maxLines="4" /> 

    </RelativeLayout> 

    <!-- Preference should place its actual preference widget here. --> 
    <LinearLayout android:id="@+android:id/widget_frame" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center_vertical" 
     android:orientation="vertical" /> 

</LinearLayout> 

您將無法直接使用這些內容,因爲所使用的一些常數是私有到Android,你就會有通過其他進一步向下挖掘XML的。

無論如何,您應該考慮到Android的偏好在不同的Android版本和不同的主題上看起來不同,所以請確保使用Android提供的常量,而不是您自己的硬編碼值,以確保您的列表視圖項目類似於Android提供的實際偏好。

+0

謝謝,這正是我正在尋找的。 – ab11 2011-04-14 13:40:24

+0

這個文件(preference.xml)和位於Android源代碼中的相關首選佈局文件在哪裏?編輯:我回答自己,更好地搜索SDK文件夾:'\ sdk \ platforms \ android-19 \ data \ res \ layout'(以「首選項」開頭的文件。 – cprcrack 2013-12-30 11:05:15

1

添加這個屬性的活動代碼,請AndroidManifest.xml

android:theme="@android:style/Preference.PreferenceScreen" 
+1

這給了我一個錯誤:'資源不是公開的' – Sam 2016-11-06 10:17:53

相關問題