How to change style of a default EditText

后端 未结 6 1719
余生分开走
余生分开走 2020-11-27 16:16

I am creating three EditTexts in my xml file using code like this:



        
相关标签:
6条回答
  • 2020-11-27 16:32

    Now For AppCompatEditText

    Note: We need to use app:backgroundTint instead of 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" />
    
    0 讨论(0)
  • 2020-11-27 16:33

    I use the below code . Check if it helps .

    <?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>
    
    0 讨论(0)
  • 2020-11-27 16:43

    I solved the same issue 10 minutes ago, so I will give you a short effective fix: Place this inside the application tag or your manifest:

     android:theme="@android:style/Theme.Holo"
    

    Also set the Theme of your XML layout to Holo, in the layout's graphical view.

    Libraries will be useful if you need to change more complicated theme stuff, but this little fix will work, so you can move on with your app.

    0 讨论(0)
  • 2020-11-27 16:46

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

    it works fine without nine-patch Api 10+

    0 讨论(0)
  • 2020-11-27 16:48

    You have a few options.

    1. Use Android assets studios Android Holo colors generator to generate the resources, styles and themes you need to add to your app to get the holo look across all devices.

    2. Use holo everywhere library.

    3. Use the PNG for the holo text fields and set them as background images yourself. You can get the images from the Android assets studios holo color generator. You'll have to make a drawable and define the normal, selected and disabled states.

    UPDATE 2016-01-07

    This answer is now outdated. Android has tinting API and ability to theme on controls directly now. A good reference for how to style or theme any element is a site called materialdoc.

    0 讨论(0)
  • 2020-11-27 16:56

    Create xml file like edit_text_design.xml and save it to your drawable folder

    i have given the Color codes According to my Choice, Please Change Color Codes As per your Choice !

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

    your Edit Text Should contain it as Background :

    add android:background="@drawable/edit_text_design" to all of your EditText's

    and your above EditText should now look like this:

          <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" />
    
    0 讨论(0)
提交回复
热议问题