有时候在布局的代码中会出现很多的重复代码,一句两句估计还是我们的承受范围之内的。要是在大工程中那
估计你就要疯了,这里我们就需要用到style属性来消除重复的代码。
首先我们看看下面的一个button布局:
<RelativeLayout
android:id="@+id/relative"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/bt_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bt_button1"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bt_button2"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bt_button3"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="@string/butoon1"/>
</RelativeLayout>
细心的你就会发现每个Button都有下面这些代码,你是否会感到繁琐
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
<resources>
<style name="styleMode">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_centerVertical">true</item>
<item name="android:layout_marginLeft">20dp</item>
<item name="android:layout_marginTop">20dp</item>
</style>
</resources>
<RelativeLayout
android:id="@+id/relative"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/bt_button1"
style="@style/styleMode"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button2"
style="@style/styleMode"
android:layout_below="@+id/bt_button1"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button3"
style="@style/styleMode"
android:layout_below="@+id/bt_button2"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button4"
style="@style/styleMode"
android:layout_below="@+id/bt_button3"
android:text="@string/butoon1"/>
</RelativeLayout>
是不是感觉比最上面的代码少了很多,在大工程中这个的设置可是非常用用的。
<RelativeLayout
android:id="@+id/relative"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/bt_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bt_button1"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bt_button2"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bt_button3"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="@string/butoon1"/>
</RelativeLayout>
当你看到每个Button都有下面这些代码的时候你是否会感到繁琐
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bt_button3"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
<resources>
<style name="styleMode">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_centerVertical">true</item>
<item name="android:layout_marginLeft">20dp</item>
<item name="android:layout_marginTop">20dp</item>
</style>
</resources>
<RelativeLayout
android:id="@+id/relative"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/bt_button1"
style="@style/styleMode"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button2"
style="@style/styleMode"
android:layout_below="@+id/bt_button1"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button3"
style="@style/styleMode"
android:layout_below="@+id/bt_button2"
android:text="@string/butoon1"/>
<Button
android:id="@+id/bt_button4"
style="@style/styleMode"
android:layout_below="@+id/bt_button3"
android:text="@string/butoon1"/>
</RelativeLayout>
是不是感觉比最上面的代码少了很多,在大工程中这个的设置可是非常用用的。