Android 如何更改默认编辑文本的样式

Android 如何更改默认编辑文本的样式,android,android-edittext,Android,Android Edittext,我正在xml文件中创建三个EditTexts,代码如下: <EditText android:id="@+id/name_edit_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/profile_image_view_layout" android:layout_centerHorizon

我正在xml文件中创建三个
EditText
s,代码如下:

<EditText
    android:id="@+id/name_edit_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/profile_image_view_layout"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="20dp"
    android:ems="15"
    android:hint="@string/name_field"
    android:inputType="text" />
      <EditText
        android:id="@+id/name_edit_text"
        android:background="@drawable/edit_text_design"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/profile_image_view_layout"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:ems="15"
        android:hint="@string/name_field"
        android:inputType="text" />

当我运行应用程序时,在我的设备上显示如下:

但我希望它看起来像这样,不使用任何背景图像:


那么如何做到这一点呢?任何想法或建议都会很有帮助。

您有一些选择

  • 用于生成需要添加到应用程序中的资源、样式和主题,以便在所有设备上获得全息外观

  • 使用图书馆

  • 将PNG用于全息文本字段,并自己将其设置为背景图像。您可以从Android资产工作室全息颜色生成器获取图像。您必须创建一个可绘制的图形,并定义正常、选定和禁用状态

  • 更新2016-01-07


    这个答案现在已经过时了。Android现在已经有了着色API和直接在控件上设置主题的能力。关于如何为任何元素设置样式或主题的一个很好的参考是一个名为的网站。

    创建类似edit_text_design.xml的xml文件,并将其保存到可绘制文件夹中

    我已经根据我的选择给出了颜色代码,请根据您的选择更改颜色代码

     <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item>
        <shape>
            <solid android:color="#c2c2c2" />
        </shape>
    </item>
    
    <!-- main color -->
    <item
        android:bottom="1.5dp"
        android:left="1.5dp"
        android:right="1.5dp">
        <shape>
            <solid android:color="#000" />
        </shape>
    </item>
    
    <!-- draw another block to cut-off the left and right bars -->
    <item android:bottom="5.0dp">
        <shape>
            <solid android:color="#000" />
        </shape>
    </item>
    
    </layer-list>
    
    
    
    您的编辑文本应将其作为背景:

    将android:background=“@drawable/edit\u text\u design”
    添加到所有EditText的

    您上面的编辑文本现在应该如下所示:

    <EditText
        android:id="@+id/name_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/profile_image_view_layout"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:ems="15"
        android:hint="@string/name_field"
        android:inputType="text" />
    
          <EditText
            android:id="@+id/name_edit_text"
            android:background="@drawable/edit_text_design"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/profile_image_view_layout"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            android:ems="15"
            android:hint="@string/name_field"
            android:inputType="text" />
    

    我使用下面的代码。检查是否有帮助

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#00f" />
                <padding android:bottom="2dp" />
            </shape>
        </item>
        <item android:bottom="10dp">
            <shape android:shape="rectangle" >
                <solid android:color="#fff" />
    
                <padding
                    android:left="2dp"
                    android:right="2dp" />
            </shape>
        </item>
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#fff" />
            </shape>
        </item>
    </layer-list>
    

    我在10分钟前解决了相同的问题,因此我将为您提供一个简短有效的解决方案: 将其放在应用程序标记或清单中:

     android:theme="@android:style/Theme.Holo"
    
    还可以在布局的图形视图中将XML布局的主题设置为Holo

    如果您需要更改更复杂的主题内容,库将非常有用,但这个小补丁会起作用,因此您可以继续使用应用程序。

    edittext\u selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/edittext_pressed" android:state_pressed="true" /> <!-- pressed -->
        <item android:drawable="@drawable/edittext_disable" android:state_enabled="false" /> <!-- focused -->
        <item android:drawable="@drawable/edittext_default" /> <!-- default -->
    </selector>
    
    
    
    edittext_default.xml

           <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#BBDEFB" />
                <padding android:bottom="2dp" />
            </shape>
        </item>
        <item android:bottom="5dp">
            <shape android:shape="rectangle" >
                <solid android:color="#fff" />
    
                <padding
                    android:left="0dp"
                    android:right="0dp" />
            </shape>
        </item>
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#fff" />
            </shape>
        </item>
    </layer-list>
    
    
    
    edittext_pressed.xml

     <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#00f" />
                <padding android:bottom="2dp" />
            </shape>
        </item>
        <item android:bottom="5dp">
            <shape android:shape="rectangle" >
                <solid android:color="#fff" />
    
                <padding
                    android:left="0dp"
                    android:right="0dp" />
            </shape>
        </item>
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#fff" />
            </shape>
        </item>
    
    </layer-list>
    
    
    
    edittext_disable.xml

        <?xml version="1.0" encoding="utf-8"?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
            <item>
                <shape android:shape="rectangle" >
                    <solid android:color="#aaaaaa" />
                    <padding android:bottom="2dp" />
                </shape>
            </item>
            <item android:bottom="5dp">
                <shape android:shape="rectangle" >
                    <solid android:color="#fff" />
    
                    <padding
                        android:left="0dp"
                        android:right="0dp" />
                </shape>
            </item>
            <item>
                <shape android:shape="rectangle" >
                    <solid android:color="#fff" />
                </shape>
            </item>
    
        </layer-list>
    
    
    
    它没有9个补丁Api 10就可以正常工作+

    现在查看
    AppCompatiEditText

    注意:我们需要使用app:backgroundTint而不是android:backgroundTint

    <android.support.v7.widget.AppCompatEditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="Underline color change"
        app:backgroundTint="@color/blue_gray_light" />
    
    
    
    如果我在任何地方都改变全息图的主题,那就行了,但是如何根据我自己的颜色改变盒子的颜色呢。我的意思是现在我把主题改成全息。因此,编辑文本在不对焦时显示灰线,在对焦时显示蓝线,因此如何自定义颜色这只是整个活动或片段上的背景图像。。。如果您需要它作为edittext的背景,恐怕您必须扩展edittext。@Ali嘿,当我使用您的第三个建议时,当我设置为edittext的背景时,我的可绘制外观被拉伸了。@anshul图像应该是patch-9图像。尤其是到materialdoc的链接对我非常有帮助,这里是指向带有edittext的条目的链接,我试过你的密码。但是所有的边框都将从编辑文本中删除。但在我的例子中,我希望在编辑文本的底部有一条蓝线,如屏幕截图所示,但编辑文本主体变为黑色..如何使其透明。实际上,在编辑文本的焦点上,我想显示蓝色,当焦点从编辑文本中移除时,我想显示灰色。如何做到这一点如果你想要一个透明的背景,这个策略是行不通的,纯色线条你可以解释发生了什么,问题是什么?问题:有人想要那种风格,但不想要图像。此解决方案:获取所需的颜色框,然后使用不同的颜色框隐藏其中的一部分(在这种情况下,必须与背景色相同,透明度在这里不起作用),以获得所需的形状。为什么我要这么做,它让我可以随心所欲地更改文本框的颜色,因为我需要创建所有这些彩色图像。我把它放在我的
    EditText
    中,效果非常好!