1. 程式人生 > >樣式主題與自定義View 淺談

樣式主題與自定義View 淺談

樣式主題自定義View

樣式和主題資源都是用於對Android應用進行美化的。 一、樣式: (一)、介紹: 1、概念:Android中的樣式和CSS樣式作用相似,都是用於為介面元素定義顯示風格,它是包含一個或者多個view控制元件屬性的集合。如:需要定義字型的顏色和大小。 2、作用:將一些常用的屬性組合成樣式,便於重複使用,減少給View控制元件指定類似屬性的重複工作。 Android Style類似網頁設計中的級聯樣式CSS設計思路,可以讓設計與內容分離,並且可以方便的繼承、覆蓋、重用。 (二)、用法: 1、儲存位置:res/values目錄下 2、寫法:
  • 以<recources>為根標籤
  • 二級節點為<style>標籤: 其中包含name屬性和parent屬性。
    • name:指定樣式的名稱;
    • parent:指定該樣式所繼承的父級樣式。當繼承於某個父樣式,那麼該樣式就獲得父樣式定義的全部格式。當然,當前樣式也可以覆蓋父樣式的格式。
  • 三級節點為<item>標籤
    • name: 該屬性值標準的控制元件屬性的格式;
    • item的文字值為該屬性所對應的值。
<resources>  <style name=“itcast”> <!-- 為樣式定義一個全域性唯一的名字-->   <item name="android:textSize">18px</item> <!-- name屬性為樣式要用在的View控制元件持有的屬性 -->   <item name="android:textColor">#0000CC</item>  </style> </resources>
<?
xml version="1.0" encoding="utf-8"?><resources><stylename="mystyle1"parent="@android:style/TextAppearance.Medium"><item name="android:layout_width">match_parent</item><item name="android:layout_height">wrap_content</item><item name="android:textColor">#00FF00</
item><item name="android:typeface">monospace</item></style></resources>
3、如何呼叫資源: <TextView     android:id="@+id/textView1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="TextView"     style="@style/mystyle1"/> 4、樣式的繼承:

有兩種方式來實現繼承:一是通過style的parent屬性,二是使用類似CSS中的命名規則來實現。

A、通過style的parent屬性繼承: <resources>     <style name="mystyle1">         <item name="android:layout_width">wrap_content</item>         <item name="android:layout_height">wrap_content</item>     </style>     <style name="mystyle2" parent="mystyle1">         <item name="android:textColor">#00FF00</item>     </style> </resources> B、通過style的命名規則繼承: ①繼承並修改:
<stylename="CodeFont.Red"><item name="android:textColor">#FF0000</item></style>
②繼承並增加屬性
<stylename="CodeFont.Red.Big"><item name="android:textSize">30sp</item></style>
③引入系統style
parent="@android:style/TextAppearance.Medium"
通過“.”號實現繼承。 引用方式:style="@style/CodeFont.Red.Big" 二、主題: (一)、概念: 1、主題和樣式資源非常相似。也是放res/values目錄下,也是以resourses作為根節點,style作為二級節點,item作為三級節點。屬性設定也相似。 2、儘管在定義上,樣式和主題基本相同,但是它們使用的地方不同。兩者區別在於:
  • 主題通過AndroidManifest.xml中的<application>和<activity>用在整個應用或者某個 Activity,主題對整個應用或某個Activity存在全域性性影響。而樣式都寫在Activity的佈局中,用在單獨的View,如:EditText、TextView等;
  • 主題定義的格式應該是改變視窗外觀的格式:例如視窗標題、視窗邊框等等。
  • 如果一個應用使用了主題,同時應用下的view也使用了樣式,那麼當主題與樣式屬性發生衝突時,樣式的優先順序高於主題。(也就是誰最靠近UI控制元件,誰起作用) 
【備註:】 Android系統也定義了一些主題,例如: <activity android:theme=“@android:style/Theme.Dialog”>, 該主題可以讓Activity看起來像一個對話方塊,如果需要查閱這些主題,可以在文件的reference-->android-->R.style 中檢視。 其它自帶的主題樣式。例如Theme.Translucent等等。 (二)、用法: 1、自定義Activity主題
<style name="MyTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullScreen">true</item>
    <item name="android:windowBackground">@drawable/star</item>
</style>
①在styles.xml中新增主題
<colorname="custom_theme_color">#b0b0ff</color><stylename="CustomTheme"parent="android:Theme.Light"><item name="android:windowBackground">@color/custom_theme_color</item><item name="android:colorBackground">@color/custom_theme_color</item></style>
②在AndroidManifest.xml中引用主題
<activityandroid:theme="@style/CustomTheme">
2.使用系統的主題
<activityandroid:theme="@android:style/Theme.Translucent">
【備註:】         Android平臺提供了一大堆樣式和主題,你完全可以在你的應用程式中使用。你可以在R.style class中找到一份關於所有可使用的樣式參考。要使用所有在這份參考中列出的樣式,你需要將樣式name的下劃線換成點號。比如,你可以通過”@android:style/Theme.NoTitleBar“來使用這裡列出的Theme_NoTitleBar主題。 三、Android設定橫屏或豎屏: (一)、全屏: 在Activity的onCreate方法中的setContentView(myview)呼叫之前新增下面程式碼 :
  • requestWindowFeature(Window.FEATURE_NO_TITLE);//隱藏標題 
  • getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);//設定全屏 
(二)、橫屏: 1、做法1:修改Activity的onResume(): @Override protected void onResume() {
 // 設定為橫屏  if(getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
 }  super.onResume();
} 2、做法2:在配置檔案中對Activity節點新增android:screenOrientation屬性(landscape是橫向,portrait是縱向) android:launchMode="singleTask" android:screenOrientation="portrait"> 3、判斷此時螢幕是橫屏還是豎屏的方法: if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {      //橫屏 } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {      //豎屏 } (三)、豎屏:         要設定成豎屏設定成 SCREEN_ORIENTATION_PORTRAIT