2011-04-13 60 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

相关问题