Setting fields for buttons using popped in android

I am developing an Android application in eclipse. I have a set of buttons, and I want to insert some space between them. I set the background of these buttons using the xml file (background.xml) in drawables. To enter spaces, I use the following lines of code for all seperatley buttons in the main xml file.

android:layout_marginLeft = "10dip" android:layout_marginRight = "10dip" android:layout_marginTop = "10dip" android:layout_marginBottom = "10dip" 

My question is Is there a way to set the fields by changing the background.xml file. Otherwise, I will have to edit all the buttons when I change the fields. Thanks in advance.

+4
source share
2 answers

This is a great example of where to use style. A style is just a group of common attributes that you want to apply to a large number of objects. For example, you can create a style called buttonStyle using the following code that will do exactly what you want. If you decide that you want to change the margin, you simply change the style. If you decide that you want to make different margin values ​​for phones of different sizes, just create two styles: one for regular, one for large and much more if necessary.

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="buttonStyle"> <item name="android:layout_marginLeft">10dip</item> <item name="android:layout_marginRight">10dip</item> <item name="android:layout_marginTop">10dip</item> <item name="android:layout_marginBottom">10dip</item> </style> </resources> 

Then the button code can be simplified to this:

 style="@style/buttonStyle" 

When you change the style, all buttons will change automatically. You can also create nested styles. See the API for more details.

+8
source

I followed a different syntax because the @PearsonArtPhoto code snippet does not work in my environment:

 <style android:id="@+id/tab_main" > <item android:layout_marginTop="10dp"></item> <item android:layout_marginBottom="10dp"></item> <item android:layout_marginLeft="10dp"></item> <item android:layout_marginRight="10dp"></item> </style> 

Hope this helps!

0
source

All Articles