【问题标题】:Android ListView customizationAndroid ListView 自定义
【发布时间】:2012-06-01 15:38:48
【问题描述】:

我有一些示例内容的 PreferenceActivity:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory
                android:title="First Category"
                android:key="first_category">
                <CheckBoxPreference
                        android:key="perform_updates"
                        android:summary="Enable or disable data updates"
                        android:title="Enable updates"
                        android:defaultValue="true"
                        />
                <ListPreference
                        android:key="updates_interval"
                        android:title="Updates interval"
                        android:summary="Define how often updates will be performed"
                        android:defaultValue="1000"
                        android:entries="@array/updateInterval"
                        android:entryValues="@array/updateIntervalValues"
                        android:dependency="perform_updates"
                        />
        </PreferenceCategory>

        <PreferenceCategory
                android:title="Second Category"
                android:key="second_category">
                <EditTextPreference
                        android:key="welcome_message"
                        android:title="Welcome Message"
                        android:summary="Define the Welcome message to be shown"
                        android:dialogTitle="Welcome Message"
                        android:dialogMessage="Provide a message"
                        android:defaultValue="Default welcome message"/>
        </PreferenceCategory>
</PreferenceScreen>

众所周知,在 4.0 之前的所有 Android 中,它看起来都非常丑陋。现在的想法是创建自定义主题并将其应用于活动。我想让 ListView 如下图所示:

我可以通过覆盖 &lt;item name="listSeparatorTextViewStyle"&gt;... 来调整分隔符(“Personalizacja”、“Informacje[...]”),但真正的问题是:如何为单独的组创建带有圆角的背景?我无法找到任何方法将自定义样式设置为组中的第一个/最后一个元素。

如果有任何想法,我将不胜感激。但请注意,我只想通过样式和主题来实现它。

顺便说一句。如果您知道如何实现上述目标,但没有圆角 - 也请告诉我!

【问题讨论】:

    标签: android listview background styles themes


    【解决方案1】:

    我认为对于一个android ListView 可以达到这个效果。 ListView 的主要功能是回收其行条目,以便不必为每个列表条目实例化它们。

    诀窍是创建多个列表元素布局并将它们交换到您的 ListAdapter 中。所以你基本上会有组头、组顶部元素、组中间元素和组底部元素的列表条目。总而言之,适配器中的 ViewHolder 应该保留 4 个不同的布局“模板”。

    下图描述了不同的布局段:

    【讨论】:

    • 您的解决方案需要使用自定义适配器,而我正在寻找的应该只是样式和主题。 PreferenceActivity 不能以这种方式设置主题。
    • 正如已经指出的那样……偏好不能被主题化。
    【解决方案2】:

    您对样式和主题的限制是一个非常严格的限制,我 99% 确定这是无法实现的,因为 PreferenceScreen 使用自己的 ListView,您无法使用样式或主题对其进行自定义。

    但是,您可以为 PreferenceScreen 中的每个项目定义 android:widgetLayout 属性,您将根据它们在组中的位置为不同的项目提供不同的背景。

    此外,位于android-sdk-windows/platforms/android-xx/data/res/ 的 Android 样式和布局文件也值得一看。

    【讨论】:

      【解决方案3】:

      您可以在父布局中设置背景颜色,并在列表视图中使用带圆角的背景图像。你可以从下面给出的代码中得到一些

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:orientation="vertical"
              android:background="#your cloue code for main bg"  >
      
         <ListView
          android:id="@+id/yourid"
          android:background="@drawable/your bg with round corner"
          android:cacheColorHint="#00000000"
          android:layout_width="fill_parent"
          android:layout_marginLeft="8dip"
          android:layout_marginTop="0dip"
          android:layout_marginRight="8dip"
          android:layout_height="wrap_content"
          android:textColor="#000000" >
      </ListView>
      </LinearLayout>
      

      【讨论】:

      • 首先,这个解决方案不仅仅是样式和主题。第二 - 它不能解决问题。使整个 ListView 有圆角是没有问题的。问题是让 ListView 中的每个组都有它们。请参考随附的屏幕截图。
      • 好的,然后在您的自定义列表视图 xml 文件中使用圆角背景图像。
      • 没有这样的东西。请再次阅读问题,并在回答之前尝试了解问题所在!
      猜你喜欢
      • 2011-09-12
      • 2015-03-28
      • 2015-06-23
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多