0

我想更改应用程序中所有编辑文本的背景形状。我像这样在drawable文件夹中创建了xml文件。

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp"/>
    <stroke android:width="1sp" android:color="@android:color/darker_gray"/>
    <solid  android:color="@android:color/white"/>
    <padding android:top="5dp" android:left="5dp" android:right="5dp" android:bottom="5dp"/>
</shape>

我在styles.xml 文件中创建了样式标签。

     <style name="editTxt_theme" parent="@android:style/Widget.EditText">
        <item name="android:background">@drawable/edittxt_shape</item>
    </style>

我将这个主题应用到我的活动中。但它应用了所有其他小部件。有什么问题吗?

谢谢。

4

1 回答 1

5

First, you need to define a theme for your app in your styles.xml and use android:editTextStyle to define your custom style for all EditText views like so:

<style name="MyAppTheme" parent="android:Theme.Light">
    <item name="android:editTextStyle">@style/CustomEditTextStyle</item>
</style>

<style name="CustomEditTextStyle">
    <item name="android:background">@drawable/edittxt_shape</item>
    <item name="android:clickable">true</item>
    <item name="android:enabled">true</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:gravity">center_vertical</item>
</style>

All that's left now is tell your app to use your theme in your manifest:

<application
    android:theme="@style/MyAppTheme" >
    ...
</application>

This way, you won't need to put style attributes on all your EditText views.

于 2013-05-17T05:23:03.910 回答