Set a consistent theme for all the editTexts in An

2019-02-25 07:04发布

问题:

I have finished making my app. Now, I want to reset all my editTexts to have the layout width as fill parent instead of wrap content.

android:layout_width="fill_parent"

while currently all my editTexts are

android:layout_width="wrap_content"

Is there any way i can do this in a style xml file, instead of individually in each layout? I currently have this as my styles.xml

<style name="AppTheme" parent="android:Theme.Light">  
    <item name="android:fontFamily">Verdana</item>
    <item name="android:editTextStyle">@style/EditTextStyle</item>
</style>

<style name="EditTextStyle" parent="@android:style/Widget.EditText">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:textColor">#808080</item>
</style>

But i'm getting an exception saying that layout_width must be specified. This is my exception:

07-15 11:13:34.872: E/AndroidRuntime(1195): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.passwordkeeper.ui/com.passwordkeeper.ui.ActivityLogin}: java.lang.RuntimeException: Binary XML file line #29: You must supply a layout_width attribute.

Any easy way out or do i have to change the attribute in all my editText's individually?

回答1:

You can try this one. Here is the part of the manifest file you need to change to call your custom theme (the custom theme called here is AppTheme:

<application android:name="YourApplication"
android:theme="@style/AppTheme" >

Then in your file styles.xml, create and customize this custom theme:

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
 <item name="android:typeface">YourTypeFace</item>
</style>

You can add the parameters you need inside the style. This will apply the style to all your textviews.



回答2:

One solution to your problem is to apply a custom theme to all of your activities. In order to do that, you can inherit properties from an existing theme and override the properties that you want to change.

  1. In AndroidManifest.xml, locate the <application> element.
  2. Add the attribute to it:

android:theme="@style/"

  1. Locate styles.xml file in the values folder.
  2. Use the following template:
<style name="ApplicationStyle" parent="android:Theme.Light">
    <item name="@android:editTextStyle">@style/customEditText</item>"
</style>

Names used in the above are just examples, you may use your own. As to the parent theme, that is also up to you.

All that is left is the definition of editTextStyle (or whatever name you have chosen for the style). You should inherit properties from Widget.EditText and override the properties that you want to change, like the following:

<style name="customEditText" parent="@android:style/Widget.EditText">
    <item name="android:textColor" >#ffffff</item>
</style>

To quote the official android guide:

The parent attribute in the element is optional and specifies the resource ID of another style from which this style should inherit properties. You can then override the inherited style properties if you want to.

I tried to make it easy to understand and follow. I'm a junior dev so while the above solution works for me, it may not be the best one out there. As I said though, it solves the problem rather efficiently.



回答3:

I was having this exact issue. For some reason it helped me to drop the android: part in the AppTheme definition, and leave it only as editTextStyle (as mentioned in this answer):

<style name="AppTheme" parent="android:Theme.Light"> 
    <item name="android:fontFamily">Verdana</item>
    <item name="editTextStyle">@style/EditTextStyle</item>
</style>


回答4:

Unfortunately it is not possible to set layout attributes (layout_*) from a theme (see Layout Parameters documentation and this answer from an Android framework engineer). You must set them on each element or set the layout attributes in a style and let each element reference the style like this:

<Button style="@style/ButtonBig" android:text="my text" />

where ButtonBig is defined like this:

<style name="ButtonBig" parent="android:Widget.Holo.Light.Button">
    <item name="android:textSize">20sp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">100dp</item>        
</style>


回答5:

You'll have to set the property style="@styles/EditTextStyle" to all of your EditText components in your application.



回答6:

define the style attribute for all EditText like below:

   <EditText
    android:id="@+id/editText1"
    android:layout_height="wrap_content"
    style="@style/EditTextStyle">


回答7:

You have to specify layout_width individually to each and every View. There is no way To escape. You can create a LayoutParams object and set its width and height in it and set in it every EditText like

textView1.setLayoutParams(lParams);
textView2.setLayoutParams(lParams);
...