【问题标题】:android:textColor not working for TextViewandroid:textColor 不适用于 TextView
【发布时间】:2019-02-27 05:44:08
【问题描述】:

android:textColor 似乎不适用于嵌套的LinearLayout 内的TextView。我查看了Textcolor selector not working 的问题。但它也不适用于我的情况。以下是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:orientation="vertical"
    tools:context=".CreateActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/lblTitle"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_weight="0.9"
            android:hint="Title:"
            android:textAlignment="textEnd"
            android:textSize="24sp"
            android:textColor="#000000"/>

        <EditText
            android:id="@+id/lblTitleEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:hint="Enter your title here"
            android:layout_weight="0.4"
            android:inputType="text"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/lblVenue"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_weight="0.9"
            android:hint="Venue:"
            android:textAlignment="textEnd"
            android:textSize="24sp"
            android:textColor="#000000"/>

        <EditText
            android:id="@+id/lblVenueEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:hint="Enter your venue here"
            android:layout_weight="0.4"
            android:inputType="text"/>

    </LinearLayout>
</LinearLayout>

我在这里犯了什么错误吗? 任何帮助将不胜感激。

【问题讨论】:

  • 使用 android:text="Title:" 代替 android:hint="Title:"
  • 感谢您的指出。我已经看过一个问题,但它对我不起作用。因此,在发布问题时,我在描述中添加了该解决方案对我不起作用的问题。无论如何,我发现了我的错误。我使用的是android:hint 而不是android:text

标签: android textview textcolor


【解决方案1】:

您在此处使用提示作为文本。因此,您必须使用以下代码将颜色应用于您的提示。或者您也可以在xml 中将提示更改为文本。

android:textColorHint="#000"

【讨论】:

    【解决方案2】:

    添加 android: 文本,你给提示属性并尝试改变文本颜色

    android:text="标题:"

    【讨论】:

      【解决方案3】:

      请检查一下

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout 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="match_parent"
          android:orientation="vertical"
          tools:context=".CreateActivity">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:layout_marginTop="15dp">
      
              <TextView
                  android:id="@+id/lblTitle"
                  android:layout_width="fill_parent"
                  android:layout_height="match_parent"
                  android:layout_gravity="end"
                  android:layout_weight="0.9"
                  android:text="Title:"
                  android:textAlignment="textEnd"
                  android:textSize="24sp"
                  android:textColor="#000000"/>
      
              <EditText
                  android:id="@+id/lblTitleEdit"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginStart="10dp"
                  android:layout_marginLeft="10dp"
                  android:hint="Enter your title here"
                  android:layout_weight="0.4"
                  android:inputType="text"/>
      
          </LinearLayout>
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:layout_marginTop="15dp">
      
              <TextView
                  android:id="@+id/lblVenue"
                  android:layout_width="fill_parent"
                  android:layout_height="match_parent"
                  android:layout_gravity="end"
                  android:layout_weight="0.9"
                  android:text="Venue:"
                  android:textAlignment="textEnd"
                  android:textSize="24sp"
                  android:textColor="#000000"/>
      
              <EditText
                  android:id="@+id/lblVenueEdit"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginStart="10dp"
                  android:layout_marginLeft="10dp"
                  android:hint="Enter your venue here"
                  android:layout_weight="0.4"
                  android:inputType="text"/>
      
          </LinearLayout>
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        对于android:text="Title:",您应该使用android:textColor="#000000"

        对于android:hint="Title:",您应该使用android:textColorHint="#000000"

        快乐编码。

        【讨论】:

          【解决方案5】:

          您正在尝试为android: hint 赋予文本颜色并将其替换为android: text

          请看下面的答案

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout 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="match_parent"
              android:orientation="vertical"
              tools:context=".CreateActivity">
          
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:layout_marginTop="15dp">
          
                  <TextView
                      android:id="@+id/lblTitle"
                      android:layout_width="fill_parent"
                      android:layout_height="match_parent"
                      android:layout_gravity="end"
                      android:layout_weight="0.9"
                      android:text="Title:"
                      android:textAlignment="textEnd"
                      android:textSize="24sp"
                      android:textColor="#000000"/>
          
                  <EditText
                      android:id="@+id/lblTitleEdit"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginStart="10dp"
                      android:layout_marginLeft="10dp"
                      android:hint="Enter your title here"
                      android:layout_weight="0.4"
                      android:inputType="text"/>
          
              </LinearLayout>
          
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:layout_marginTop="15dp">
          
                  <TextView
                      android:id="@+id/lblVenue"
                      android:layout_width="fill_parent"
                      android:layout_height="match_parent"
                      android:layout_gravity="end"
                      android:layout_weight="0.9"
                      android:text="Venue:"
                      android:textAlignment="textEnd"
                      android:textSize="24sp"
                      android:textColor="#000000"/>
          
                  <EditText
                      android:id="@+id/lblVenueEdit"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginStart="10dp"
                      android:layout_marginLeft="10dp"
                      android:hint="Enter your venue here"
                      android:layout_weight="0.4"
                      android:inputType="text"/>
          
              </LinearLayout>
          </LinearLayout>
          

          【讨论】:

            【解决方案6】:

            您在文本视图中设置的是提示文本。所以,你需要设置textColorHint text-view 的属性。

            您所做的是在 text 上设置颜色。这不起作用,因为您没有在 text-view 中输入任何文本。

                 <TextView
                    android:id="@+id/lblTitle"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="end"
                    android:layout_weight="0.9"
                    android:hint="Title:"
                    android:textAlignment="textEnd"
                    android:textSize="24sp"
                    android:textColorHint="#000000"/>
            

            【讨论】:

              【解决方案7】:

              根据您的代码,您在 Textview 中编写了提示,因此对于提示使用中的应用颜色,

              android:textColorHint="#000000"
              

              或者如果你想使用文本颜色,那么你必须将文本设置到你的 Textview 中,如下所示:

              android:text="Title:"
              android:textColor="#000000"
              

              如果您在 TextView 中使用提示,则使用如下:

              android:hint="Title:"
              android:textColorHint="#000000"
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-02-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多