Android:为什么ListView中的字幕不是灰色的?

标签 android listview

我使用最新的Android Studio和SDK。在预览和真实设备中我看到了这个:

enter image description here

我的代码:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.myappname.view.AboutActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listViewAbout" />

</RelativeLayout>

如何使字幕文字颜色为灰色?像这样:

enter image description here

最佳答案

我冒险假设您使用的是行布局 simple_list_item_2.xml(基于屏幕截图),它为您提供了两行。 问题(如果您可以这样调用它)是,根据 SDK 版本,styling因为此布局已更改。

关于SDK 23 ,看起来像这样:

enter image description here

但是,说 SDK 19 ,看起来像这样:

enter image description here

为什么?

要理解这一点,我们首先需要查看从 simple_list_item_2.xml 生成行的 xml,您会看到它是一个非常简单的布局,使用现已弃用的 View TwoLineListItem 但这只是为什么要使用自定义布局的一个优点。

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/listPreferredItemHeight"
    android:mode="twoLine"
    android:paddingStart="?attr/listPreferredItemPaddingStart"
    android:paddingEnd="?attr/listPreferredItemPaddingEnd">

    <TextView android:id="@id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textAppearance="?attr/textAppearanceListItem" />

    <TextView android:id="@id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        android:layout_alignStart="@id/text1"
        android:textAppearance="?attr/textAppearanceListItemSecondary" />

</TwoLineListItem>

原因是每个 SDK 版本中解析样式 textAppearanceListItemSecondary 的方式不同。样式决定了文本的大小、颜色等。Android 界面的发展催生了一个巨大的主题生态系统,依赖默认样式会导致不一致,就像您偶然发现的那样。

该怎么办?

您应该为此使用自己的布局,以便跨版本实现统一的样式。为此,请参阅 multiple questions 中的任何一个涵盖此事。但简而言之,它只是意味着创建一个布局文件,将其命名为 custom_row.xml 并让布局看起来完全符合您的要求。这还使您可以完全控制项目的放置、您可能需要的额外 View ,并且与您可能使用的 SimpleAdapterArrayAdapter 相比,编码方面的开销是最小的正在使用。

注意

如果您还没有这样做,您应该考虑将代码移向 RecyclerView 而不是 ListView

关于Android:为什么ListView中的字幕不是灰色的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36909014/

相关文章:

c# - WPF ListView 通过标题模板与扩展器分组?

android - 如何使用 Visual Studio 将 Android 设备连接到 Azure VM

android - 如何创建 xml 的 id?

java - 如何使用 Arraylist<Hashmap<String,String>> 中的 bundle 使用 putserializable 获取传递的值

listview - flutter 。在非常大的 ListView 上设置 ListView 初始位置

wpf - Observable Collection 不更新 UI

C++ ListView 工具提示

android - 如何在 RN for Android 中禁用字体缩放?

android - Dialog 的键盘消失后,VideoView 未正确呈现

delphi - 如何更改ListView OnDrag图像?