一門技術的基礎知識是掌握一門技術的起點和支撐。許多基礎知識需要我們不斷的回顧。
我們經常需要對TextView、Button的控件的樣式做一些修改,比如圓角、填充色、邊框色等
在對控件的形狀以及狀態變化等處理的離不開<shape>和<selector>
關于<shape>和<selector>之間的區別這里有一篇文章總結的很不錯 http://blog.csdn.net/brokge/article/details/9713041/
1、 圓角邊框實現
在drawable中新建一個xml文件
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <!--角度--> <corners android:radius="5dp" /> <!--填充色--> <solid android:color="#FF3030" /> <stroke android:color="#000000" android:width="1dp" /></shape>在布局文件中引用這個資源文件(作為Background)<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:textColor="#FFFFFF" android:background="@drawable/style_textview_normal" android:text="主題"/>效果如圖:
為什么這樣定義?
這個實際是給TextView定義了一個背景,Android 控件(TextView、Button。。)的背景background可以接受許多種資源類型,color、drawable
2、樣式的復用
在style.xml中定義一個樣式
<style name="style_text_array"> <item name="android:textSize">21dp</item> <item name="android:textColor">#FFFFFF</item> <item name="android:background">@drawable/style_textview_normal</item> </style>在布局文件中引用
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/style_text_array" android:text="主題"/>樣式復用相當于統一的定義了一個樣式組,可以在許多的控件里引用,從而減少重復代碼
新聞熱點
疑難解答