简体   繁体   English

android:textColor不适用于TextView

[英]android:textColor not working for TextView

android:textColor doesn't seem to work for TextView inside a nested LinearLayout . android:textColor似乎不适用于嵌套的LinearLayout TextView I had a look at the question at Textcolor selector not working . 我看看Textcolor选择器不起作用的问题。 But it is also not working in my case. 但这对我来说也不起作用。 Following is my layout file: 以下是我的布局文件:

<?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>

Am I making any mistake here? 我在这里犯错了吗? Any help would be greatly appreciated. 任何帮助将不胜感激。

You used hint as text here. 您在此处使用提示作为文本。 So you have to apply color to your hint with the following code. 因此,您必须使用以下代码将颜色应用于提示。 or you can also change hint to text in your xml . 或者,您也可以将提示更改为xml文本。

android:textColorHint="#000"

Add android: text, you give hint property and try to text color change 添加android:文本,您给提示属性,然后尝试更改文本颜色

android:text="Title:" android:text =“标题:”

Please check this 请检查一下

<?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>

For android:text="Title:" you should use android:textColor="#000000" 对于android:text="Title:"您应该使用android:textColor="#000000"

And

For android:hint="Title:" you should use android:textColorHint="#000000" 对于android:hint="Title:"您应该使用android:textColorHint="#000000"

Happy Coding. 编码愉快。

You are trying to give text color to android: hint and replace it with android: text . 您正在尝试将文本颜色赋予android: hint ,并将其替换为android: text

See the answer below 见下面的答案

<?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>

Here what you set in text view is hint text. 在这里,您在文本视图中设置的是提示文本。 So , you need to set textColorHint property of text-view. 因此,您需要设置text-view的textColorHint属性。

What you was doing is setting color on text . 您正在做的是在文本上设置颜色。 Which will not work because you haven't entered any text in text-view . 由于您未在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"/>

As per your code, you wrote hint in a Textview so for the apply color in hint use like, 根据您的代码,您在Textview中编写了提示,因此对于提示中的套用颜色,例如,

android:textColorHint="#000000"

Or if you want to use text color then you have to set text into your Textview as below: 或者,如果要使用文本颜色,则必须将文本设置为Textview,如下所示:

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

If you are using a hint in a TextView then use as below: 如果您在TextView中使用提示,则使用以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM