【问题标题】:TextView text color in complex ListView item differs across devices复杂 ListView 项中的 TextView 文本颜色因设备而异
【发布时间】:2019-03-10 19:18:04
【问题描述】:

我有一个 ListView 和一个仅由 TextView 和 Button 组成的自定义项目布局。实际的 ListView 工作正常,但在我测试的不同设备上显示不同。

在我的 Android 8.0 设备上,文本为深灰色,但在我的 6.0 设备上,文本为浅灰色,在我的 Activity 的白色背景下几乎看不到。此外,该设备上的按钮颜色也会反转,在 8.0 中,浅色按钮上的文本为深色,而在 6.0 中则相反。

<RelativeLayout            
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cell_peer"
    android:descendantFocusability="blocksDescendants"
    android:padding="3dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/text_peer_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="TextView"
        tools:text="@tools:sample/full_names" />

    <Button
        android:id="@+id/button_call_peer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Call"
        app:layout_constraintEnd_toEndOf="parent"
        />
</RelativeLayout>

清单:

<activity android:name=".activities.FriendsListActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="Call a friend"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme.NoActionBar"
    ></activity>

我可以通过在 TextView 上设置 android:textColor="@color/color_callable_peer_name" 来轻松解决文本对比度问题,但我的问题是:为什么此文本在不同平台上默认为不同颜色?

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    因为您可以轻松做到这一点。

    public class MyTextView extends TextView {
        public MyTextView (Context context) {
            super(context);
            if (android.os.Build.VERSION.SDK_INT == ......Marshmallow){
                // set text color you wanted}
            else
            {
                //set text color you wanted
            }
        }     
    }
    

    在您的 xml 文件中使用 MyTextView 而不是 TextView。

    【讨论】:

    • 这并没有回答我的问题“为什么这个文本在不同的平台上默认为不同的颜色?”。我不是想找到一种在不同平台上设置不同颜色的方法,我希望它在所有平台上都相同,并且想知道我做错了什么
    猜你喜欢
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多