Android 中的Theme和Style使用

來源:互聯網
上載者:User

Android 中的Theme和Style使用,還是比較簡單的。

1、 首先在res/values/styles.xml的resource中定義三個樣式,分別為:

 <style name="TextView">  <item name="android:textSize">38sp</item>  <item name="android:textColor">#128</item>  <item name="android:shadowRadius">1.0</item>  <item name="android:background">#035</item> </style> <style name="EditText">  <item name="android:shadowColor">@android:color/black</item>  <item name="android:shadowRadius">1.0</item>  <item name="android:background">#312</item>  <item name="android:foreground">#432</item>  <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>  <item name="android:height">80dp</item> </style><style name="Button">    <item name="android:background">@android:drawable/edit_text</item>    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item></style>

 然後在res/layout檔案下的activity_main.xml中的控制項中引用剛才定義的Style。

    <TextView        android:id="@+id/textView1"        style="@style/TextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Button        android:id="@+id/button1"        style="@style/Button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView1"        android:layout_below="@+id/editText1"        android:layout_marginTop="18dp"        android:text="@string/hello_world" />    <EditText        android:id="@+id/editText1"        style="@style/EditText"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/button1"        android:layout_below="@+id/textView1"        android:layout_marginTop="19dp"        android:ems="10"        android:text="@string/hello_world" />

 

2、Android的Theme的使用

首先在res/values/themes.xml中定義Theme。

<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android">    <style name="Theme" parent="android:Theme.Light">        <item name="android:windowFullscreen">true</item>        <item name="android:windowTitleSize">60dip</item>        <item name="android:windowTitleStyle">@style/WindowTitle</item>        <item name="android:background">#234</item>    </style>    <style name="WindowTitle">        <item name="android:singleLine">true</item>        <item name="android:shadowColor">#658</item>        <item name="android:shadowRadius">2.75</item>    </style>  </resources>

 然後在AndroidManifest.xml中使用剛才定義的主題。

只要定義application的android:theme屬性為style/Theme即可。

    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/Theme" >        <activity            android:name="com.example.themedemo.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>

 也可以用setTheme(R.style.Theme)來調用主題。就不上傳了。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.