android Style(样式)的解析
生活随笔
收集整理的這篇文章主要介紹了
android Style(样式)的解析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先說下為啥要使用樣式吧,舉個例子來說吧
當要做這樣的一個布局的時候
每個標簽都要寫的話,會感覺怎樣
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal" ><Buttonandroid:id="@+id/bt1"android:layout_width="match_parent"android:layout_height="35sp"android:layout_marginLeft="6dp"android:layout_marginRight="10dp"android:layout_weight="1"android:background="#009999"android:text="上班族"android:textColor="#ffffff"android:textSize="11sp" /><Buttonandroid:id="@+id/bt2"android:layout_width="match_parent"android:layout_height="35sp"android:layout_marginRight="10dp"android:layout_weight="1"android:background="#009999"android:text="醫療"android:textColor="#ffffff"android:textSize="11sp" /><Buttonandroid:id="@+id/bt3"android:layout_width="match_parent"android:layout_height="35sp"android:layout_marginRight="10dp"android:layout_weight="1"android:background="#009999"android:text="媒體"android:textColor="#ffffff"android:textSize="11sp" /><Buttonandroid:id="@+id/bt4"android:layout_width="match_parent"android:layout_height="35sp"android:layout_marginRight="6dp"android:layout_weight="1"android:background="#009999"android:text="白領"android:textColor="#ffffff"android:textSize="11sp" /></LinearLayout>
要寫很多這樣的,不停的重復寫是不是很煩呢,為了方便我們 要學習樣式了,
樣式資源
首先要明白樣式資源是用來干啥的,
樣式資源主要用于對組件的顯示樣式經行控制,改變文本框顯示文字的大小和顏色
位置:
res/values 目錄中,
它的根元素是<resources></resources>標記,
在該元素中,使用<style></style>標記定義樣式,
做的時候到values下找到styles
我把上面的代碼簡化下
這個就是要在xml中寫的提前用style寫好這樣在
xml中不用寫了
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal" ><Buttonandroid:id="@+id/bt1"style="@style/button"android:layout_marginLeft="10dp"android:text="上班族" /><Buttonandroid:id="@+id/bt2"style="@style/button"android:text="醫療" /><Buttonandroid:id="@+id/bt3"style="@style/button"android:text="媒體" /><Buttonandroid:id="@+id/bt4"style="@style/button"android:text="白領" /></LinearLayout> 它的效果圖
這樣是不是很方便了,樣式的好處就是一次寫好多出都能調用
下面的標簽也是能調用的?
總結
以上是生活随笔為你收集整理的android Style(样式)的解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LinearLayout (线性布局)的
- 下一篇: RelativeLayout(相对布局)