gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:48:16 26 4
gpt4 key购买 nike

我使用最新的 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>

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

怎么办?

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

注意事项

如果您还没有将代码移至 RecyclerView 而不是 ListView,则应考虑将其移至 RecyclerView

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com