如何更改android中的编辑文本底线颜色?

时间:2015-06-02 04:30:27

标签: android xml colors background

Hi I am doing a login page for my project whose xml looks like this


            <EditText 
                android:id="@+id/user_name_id"
                android:layout_height="wrap_content"
                android:layout_width="300dp"
                android:ems="10"
                android:singleLine="true"
                android:layout_gravity="top|center"
                android:gravity="center"
                android:layout_marginTop="200dp"
                android:hint=" Email Address"
                android:textColor="#B3B3B3"
                android:textColorHint="#B3B3B3"
                android:textCursorDrawable="@drawable/color_cursor"
                />

about代码显示文本,提示和光标位于#b3b3b3颜色代码中。我希望底线也使用相同的颜色代码。我也尝试使用选择器,如下所示。但是对我来说很有帮助。请提出一个真正的方法来特别改变edittext的底线颜色。

                    <selector 
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    >
                    <item 
                        android:state_pressed="true"
                        android:drawable="@android:drawable/edittext_pressed" 
                        /> <!-- pressed -->    
                    <item 
                        android:state_focused="true"
                        android:drawable="@drawable/edittext_focused_blue" 
                        /> <!-- focused -->    
                    <item 
                        android:drawable="@android:drawable/edittext_normal" 
                        /> <!-- default -->
                    </selector>

我使用xml中的以下代码来更改光标颜色

            <?xml version="1.0" encoding="utf-8"?>
            <shape xmlns:android="http://schemas.android.com/apk/res/android" >
                <size android:width="1dp" />
                <solid android:color="#B3B3B3"  />
            </shape> 

或者任何人都可以为我修改以下代码

            <resources>

                <!--
                    Base application theme, dependent on API level. This theme is replaced
                    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
                -->
                <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
                    <!--
                        Theme customizations available in newer API levels can go in
                        res/values-vXX/styles.xml, while customizations related to
                        backward-compatibility can go here.
                    -->
                </style>

                <!-- Application theme. -->
                <style name="AppTheme" parent="AppBaseTheme">
                    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
                </style>

            </resources>

3 个答案:

答案 0 :(得分:1)

我正在使用api level 21(lollypop),我找到了解决方案,因为此解决方案仅适用于api级别21及以上。使用图层列表

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item>
    <shape android:shape="line" >
        <solid android:color="#B3B3B3" />
    </shape>
</item>

<item android:top="-2dp" android:right="-2dp" android:left="-2dp">
    <shape>
        <solid android:color="@android:color/transparent" />
        <stroke
            android:width="0.1dp"
            android:color="#B3B3B3" />
    </shape>
</item>

</layer-list>

如果用于低于21的api级别则会出现异常。

答案 1 :(得分:0)

如果您正在使用v21 +(lollipo)支持库,则可以在应用主题中设置colorAccent:

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
    ...
    <item name=”colorAccent”>@color/accent</item> 
    ...
</style>

答案 2 :(得分:0)

使用app-compact库

<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">#c5c5c5</item>
<item name="colorControlActivated">@color/accent</item>
<item name="colorControlHighlight">@color/accent</item>

现在,您的应用中的Edittext线条颜色会发生变化。