微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android:如何制作类似于首选项的ListView?

如何解决Android:如何制作类似于首选项的ListView?

| 我不想使用框架提供的Preferences,而是想创建一个看起来类似的ListView。特别是,我希望它为TextView使用相同的字体大小和样式。     

解决方法

这与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提供的实际偏好设置。     ,将此属性添加到您的“ 1”中的活动标签
 android:theme=\"@android:style/Preference.PreferenceScreen\"
    ,试试这个http://android-journey.blogspot.com/2010/01/for-almost-any-application-we-need-to.html     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。