【问题标题】:Color of TextView shows as white on a real device with ConstraintLayoutTextView 的颜色在具有 ConstraintLayout 的真实设备上显示为白色
【发布时间】:2017-07-22 15:40:56
【问题描述】:

我一直在尝试使用ConstraintLayout,到目前为止我非常喜欢它很多,但现在我遇到了一个特殊的问题。相同的布局,如果我使用RelativeLayout 进行设计,则将包含的TextViews 的颜色显示为默认文本颜色(灰色),但使用ConstraintLayout,则显示为白色。

这发生在真实设备和带有 Android 5.0 的模拟器上,但在模拟器 (7.0) 上没有。有谁知道,为什么会发生这种情况?

此外,显而易见的解决方案似乎是手动将布局中 TextView 的颜色设置为应有的颜色,但这似乎是一种技巧,而不是一种解决方案。我很想知道,为什么会发生这种情况,如果这个黑客是唯一的解决方案? (我不喜欢通过在 TextView 上强制使用颜色来修复它,因为这种行为在 Android 7.0 上是正确的

编辑:忘了提到布局编辑器将它们都显示为黑色/灰色,因此这也是实际颜色不应为白色的提示。


供您参考,这里有两个或多或少相同的布局(其中一个有 Button,另一个有 TextView 而不是 Button)哪个有问题。

显示为白色的布局(焦点问题):

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginEnd="8dp"
    android:background="@color/lightGrey">


    <android.support.constraint.ConstraintLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:paddingBottom="10dp"
        android:elevation="2dp"
        android:id="@+id/constraintLayout">


        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/guideline"
            android:layout_marginRight="8dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintHorizontal_bias="0.0"
            android:id="@+id/linearLayout"
            tools:layout_editor_absoluteY="16dp">

            <TextView
                android:text="@string/tutor_name"
                android:textStyle="bold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/tutor_name"/>

            <TextView
                android:text="@string/tutor_skill_set"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:id="@+id/skill_set"/>

            <TextView
                android:text="@string/tutor_types"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:id="@+id/tutor_types" />

            <TextView
                android:text="@string/tutor_location"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/location"
                android:layout_marginTop="12dp" />

        </LinearLayout>


        <ImageView
            android:id="@+id/display_pic"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginEnd="16dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="16dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            android:adjustViewBounds="false"
            android:scaleType="centerCrop"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintLeft_toLeftOf="@+id/guideline"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:color/holo_red_light" />

        <com.iarcuschin.simpleratingbar.SimpleRatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tutor_rating"
            android:layout_below="@+id/display_pic"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            app:srb_starSize="13dp"
            app:srb_numberOfStars="5"
            app:srb_borderColor="@color/colorAccent"
            app:srb_fillColor="@color/colorPrimary"
            app:srb_starBorderWidth="1"
            app:srb_isIndicator="true"
            app:layout_constraintRight_toRightOf="@+id/display_pic"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/display_pic"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintLeft_toLeftOf="@+id/guideline"
            app:layout_constraintHorizontal_bias="1.0" />

        <android.support.constraint.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/guideline"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.6796875" />


    </android.support.constraint.ConstraintLayout>


    <TextView
        android:id="@+id/tutor_requested_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="32dp"
        android:text="Requested time"
        android:textStyle="italic"
        android:layout_marginStart="32dp"
        android:layout_marginBottom="8dp"
        android:layout_below="@+id/constraintLayout" />


</RelativeLayout>

显示为黑色/灰色的布局(应为默认)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginEnd="10dp"
    android:background="@color/lightGrey"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:elevation="2dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:paddingBottom="10dp"
        android:paddingEnd="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingStart="10dp"
        android:paddingTop="10dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1"
            android:layout_toLeftOf="@+id/display_pic"
            android:layout_toStartOf="@+id/display_pic"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:id="@+id/linearLayout2">

            <TextView
                android:text="@string/tutor_name"
                android:textStyle="bold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/tutor_name"/>

            <TextView
                android:text="@string/tutor_skill_set"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:id="@+id/skill_set"/>

            <TextView
                android:text="@string/tutor_types"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:id="@+id/tutor_types" />

            <TextView
                android:text="@string/tutor_location"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/location"
                android:layout_marginTop="12dp" />

        </LinearLayout>


        <ImageView
            android:id="@+id/display_pic"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:adjustViewBounds="false"
            android:scaleType="centerCrop"
            app:srcCompat="@android:color/holo_red_light" />

        <com.iarcuschin.simpleratingbar.SimpleRatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tutor_rating"
            android:layout_below="@+id/display_pic"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            app:srb_starSize="13dp"
            app:srb_numberOfStars="5"
            app:srb_borderColor="@color/colorAccent"
            app:srb_fillColor="@color/colorPrimary"
            app:srb_starBorderWidth="1"
            app:srb_isIndicator="true"
            android:layout_marginTop="2dp" />

        <Button
            android:id="@+id/request_tutor"
            style="@android:style/Widget.Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/linearLayout2"
            android:layout_marginTop="14dp"
            android:background="@color/lighterGrey"
            android:minHeight="25dp"
            android:minWidth="80dp"
            android:text="Request"
            android:textAppearance="@style/TextAppearance.AppCompat" />

    </RelativeLayout>

</FrameLayout>

注意:虽然我尝试在下面使用 Barrier,但它在 ConstraintLayout 1.1.0 beta 中,在这种情况下我会严格避免使用 beta 版本进行生产) em>

编辑 2: 遵循 @rami-jemli 的建议,这里是 ConstraintLayout,但问题仍然存在。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginEnd="8dp"
    android:background="@color/lightGrey"
    android:id="@+id/constraintLayout">


    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/guideline"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintHorizontal_bias="0.0"
        android:id="@+id/linearLayout">

        <TextView
            android:text="@string/tutor_name"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tutor_name"/>

        <TextView
            android:text="@string/tutor_skill_set"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:id="@+id/skill_set"/>

        <TextView
            android:text="@string/tutor_types"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:id="@+id/tutor_types" />

        <TextView
            android:text="@string/tutor_location"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/location"
            android:layout_marginTop="12dp" />

    </LinearLayout>


    <ImageView
        android:id="@+id/display_pic"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:adjustViewBounds="false"
        android:scaleType="centerCrop"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="@+id/guideline"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:color/holo_red_light" />

    <com.iarcuschin.simpleratingbar.SimpleRatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tutor_rating"
        android:layout_below="@+id/display_pic"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        app:srb_starSize="13dp"
        app:srb_numberOfStars="5"
        app:srb_borderColor="@color/colorAccent"
        app:srb_fillColor="@color/colorPrimary"
        app:srb_starBorderWidth="1"
        app:srb_isIndicator="true"
        app:layout_constraintRight_toRightOf="@+id/display_pic"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/display_pic"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintLeft_toLeftOf="@+id/guideline"
        app:layout_constraintHorizontal_bias="1.0" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.6796875"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="250dp" />

    <TextView
        android:id="@+id/tutor_requested_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Requested time"
        android:textStyle="italic"
        android:layout_below="@+id/constraintLayout"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="@+id/barrier"
        app:layout_constraintStart_toStartOf="@+id/linearLayout"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="tutor_rating,linearLayout"
        tools:layout_editor_absoluteY="126dp" />


</android.support.constraint.ConstraintLayout>

输出:(虚拟数据)

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    我简直不敢相信!我找到了答案。

    从问题中,无法确定可能出了什么问题,因为问题不在于 布局RecyclerViewAppCompatConstraintLayout。问题在于称为主题上下文的东西。我几乎在所有情况下都在传递应用程序上下文,但事实证明,在涉及布局的地方,建议传入 Activity 实例本身(这是一个主题上下文,因为它扩展了 ContextThemeWrapper --more稍后再说)。

    因此,例如,当我们为RecyclerView 实例化LayoutManager 时,我们将传入Activity 实例。当我们为 RecyclerView 扩展布局项时,我们将再次使用 Activity

    在这种情况下,ContextThemeWrapper 具有概念上的优先级,正如 hereActivity 的子类。

    另外提到的是,

    ContextThemeWrapper 将应用您的应用程序主题

    我上面分享的代码的 sn-p 并没有立即实现 显然,但是,您可以编写自己的测试来验证这一点。最好的 我能想到的方法是创建一个简单的应用程序 使用android.R.layout.simple_list_item_1 显示的ListView 项目。使您的应用程序主题默认为 Light,然后初始化您的 ArrayAdapter 使用 getApplicationContext()。你会注意到 文本不可见或几乎不可见,因为它将是白色的 默认情况下,您的主题应该要求默认文本 黑色的。更改代码,以便您的代码初始化数组 带有“this”的适配器(即Activity)或简单的getContext()和 你会看到文字变黑。

    还有,

    不要将getApplicationContext()LayoutInflater 一起使用,除非你真的想忽略你的主题。


    向@sockeqwe 致敬,因为他的answer on SwitchCompat 给了我这个想法。

    注意:之前,我曾提到这可能是ConstraintLayout 中的一个错误。我收回这一点并向在 ConstraintLayout 工作的人们道歉(我觉得这太棒了!

    最后,感谢所有抽出宝贵时间阅读我的问题并为解答而绞尽脑汁的人。

    【讨论】:

    • 很高兴您能找到您的问题。你能接受你的答案,让以后发现这个问题的人明白吗?
    • @Gary99:当然。完成。
    【解决方案2】:

    您现在使用ConstraintLayout 的方式是错误的。你没有得到它的性能优势。不需要所有其他 ViewGroup(RelativeLayout 等)和嵌套布局。
    在您的情况下,您只需要使用一个 ConstraintLayout 作为没有嵌套级别的父布局。
    使用Chains 而不是LinearLayout
    尝试实现这一目标并使用新代码更新您的问题。
    让我们看看你得到了什么。

    【讨论】:

    • 嗯,这就是我最初的计划,但在这样做时,我遇到了 ConstraintLayout 的问题。你看,我可以拥有每个视图边缘(用于顶部和底部边缘)只有 1 个垂直约束,但我希望我的视图位于其他 2 个视图的底部边缘下方。这是我用平面 ConstraintLayout 做不到的。为了给你一个想法,请参阅:imgur.com/a/sw5bx请求的时间 TextView 必须在 LinearLayout 的底部以及右侧的评分栏下方对齐。您对此有解决方案吗?
    • 要将一个视图保持在其他两个视图的边缘下方,您可以使用障碍。它们是 ConstraintLayout 1.1.0-beta1 版本的一部分。您仍然可以获得平面布局。试一试。
    • 我在这里解释了障碍 (stackoverflow.com/questions/44487758/…)
    • 是的......在你提到之后,我看到了障碍的演练。事实证明,对 1.1.0 beta 的简单约束布局更新不会在视觉上促进 Barriers 的使用,因此我现在正在下载 AS 3.0 Canary-5 Preview 以使用它。但是非常感谢您指出我这个方向。 +1
    • 我成功设置了屏障,但白色的错误仍然存​​在......;(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多