【问题标题】:How to change the text color for the InputTextLayout in android如何在android中更改InputTextLayout的文本颜色
【发布时间】:2018-05-07 03:40:04
【问题描述】:

编辑: 我想摆脱的酒吧是顶部的酒吧,上面写着测试。我要更改的文本颜色是用户输入的“testing@gmail.com”文本:

这是我用于登录活动的 XML(我使用了 android studio 提供的默认登录活动):

<LinearLayout 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"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.uname.myapp.LoginActivity">

    <!-- Login progress -->
    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />

    <ScrollView
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/email_login_form"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <AutoCompleteTextView
                    android:id="@+id/email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_email"
                    android:inputType="textEmailAddress"
                    android:maxLines="1"
                    android:singleLine="true" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_password"
                    android:imeActionId="6"
                    android:imeActionLabel="@string/action_sign_in_short"
                    android:imeOptions="actionUnspecified"
                    android:inputType="textPassword"
                    android:maxLines="1"
                    android:singleLine="true" />

            </android.support.design.widget.TextInputLayout>

            <Button
                android:id="@+id/email_sign_in_button"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="@string/action_sign_in"
                android:textStyle="bold" />

        </LinearLayout>
    </ScrollView>
</LinearLayout>

我想删除顶部的栏,所以在线显示的解决方案是从Theme.AppComtact.NoActionBar继承。这是我的风格:

<style name="MyTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:textColorHint">@android:color/darker_gray</item>
        <item name="android:textColor">@android:color/black</item>
    </style>

我在TextInputLayout 上找不到文档以及在文本字段中键入时如何简单地更改字体颜色而感到沮丧。尝试在 SO 和其他网站上搜索,但找不到解决方案。

我什至尝试过&lt;item name="android:colorControlNormal"&gt;@android:color/black&lt;/item&gt;,但它需要 API 级别 21,现在最低要求 15。

来自 Python / Django 背景,第一次尝试学习 android 开发。

谁能指出我正确的方向?是否有文档显示开发人员可以在文本上应用的所有可能的样式?

我的androidmanifest 文件在应用程序中有这个:

android:theme="@style/MyTheme"

【问题讨论】:

    标签: android android-layout android-studio android-styles android-studio-3.0


    【解决方案1】:

    如果您想删除操作栏或标题栏,请使用此样式并根据您的需要设置必要的真/假。

    <style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
        <item name="android:windowNoTitle">false</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
    

    在你的清单文件中

    <activity
       android:name=".activities.FullViewActivity"
       android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" 
    />
    

    用于更改字体颜色

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Your Name"
        android:textColor="#FF0000"
    </EditText>
    

    【讨论】:

    • 我们可以将更改 InputTextLyout 小部件颜色放入样式中吗?我希望它在应用程序范围内。对于您当前的解决方案,我将&lt;EditText ... 代码放入哪个文件?
    • 另外,删除操作栏的代码很遗憾没有成功删除我要删除的栏。我更新了我的问题以显示我想要删除的栏。请看一看。谢谢。
    • 我创建了样式parent="@style/Theme.AppCompat.Light.NoActionBar" 的父级,它成功地移除了操作栏。感谢您对操作栏的帮助!
    • 欢迎。您可以将电子邮件输入从 更改为
    • 是的,转到 values 文件夹然后 colors.xml 然后插入你的颜色,例如&lt;color name="colorPrimary"&gt;#e91e63&lt;/color&gt; 然后稍后使用它 android:textColor="@color/colorPrimary" 并用于特定样式,例如&lt;style name="AppTheme" parent="Theme.AppCompat.NoActionBar"&gt; &lt;item name="colorPrimary"&gt;@color/colorPrimary&lt;/item&gt; &lt;item name="colorPrimaryDark"&gt;@color/colorPrimaryDark&lt;/item&gt; &lt;item name="colorAccent"&gt;@color/colorAccent&lt;/item&gt; &lt;item name="drawerArrowStyle"&gt;@style/DrawerArrowStyle&lt;/item&gt; &lt;/style&gt;
    【解决方案2】:

    如果您只想设置 TextInputLayout 提示颜色,那么您应该试试这个

    app:hintTextAppearance="@style/text_appearance_color"

    <android.support.design.widget.TextInputLayout
            android:padding="8dp"
            android:id="@+id/tilSample"
            app:errorTextAppearance="@style/error_appearance"
            app:hintTextAppearance="@style/text_appearance_color"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <EditText
                android:id="@+id/etSample"
                android:layout_margin="8dp"
                android:padding="8dp"
                android:hint="android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </android.support.design.widget.TextInputLayout>
    

    对于 NoActionBar,您的代码似乎没问题,只需在应用程序级别的清单中应用主题即可。

    编辑: 风格

    <style name="editTextStyle" parent="@android:style/Widget.EditText">
        <item name="android:background">@drawable/someBackground</item>
        <item name="android:textColor">#808080</item>
        <item name="android:layout_height">45dip</item>
        <!--other attibutes goes here-->
    </style>
    

    【讨论】:

    • 我不想更改提示颜色,我已经使用&lt;item name="android:textColorHint"&gt;@android:color/darker_gray&lt;/item&gt; 更改提示颜色。当用户开始在输入文本框内输入时,颜色为白色(在白色背景上)。我希望更改用户开始在输入文本框中输入时出现的文本颜色。
    • android:textColor="#000" 这不起作用,用于更改文本颜色?
    • android:textColor="#000" 仅在我将其放在&lt;EditText&gt; / &lt;TextInputLayout&gt; 标记中的 XML 文件中时才有效。我希望能够从样式中控制文本颜色,因为我在整个应用程序中有多个 &lt;EditText&gt; / &lt;TextInputLayout&gt; 小部件,并且必须将 android:textColor="#000" 添加到每个小部件似乎不是一个安全的解决方案。是否可以更“全局”地更改颜色?喜欢来自styles.xml 文件/主题?
    • @vikarkumar 创建全新样式的问题是,我的应用程序已经使用了一个名为MyTheme 的主题。主题在styles.xml 中使用style 标签进行了描述。如果我创建另一个名为editTextStyle 的样式继承自@android:style/Widget.EditText,我如何告诉我的应用程序也使用editTextStyle 主题/样式(除了使用MyTheme)?它不允许我在我的应用清单中有两个 android:theme="@style/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 2023-03-31
    • 1970-01-01
    • 2012-09-24
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多