Android 中的Theme和Style使用

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)来调用主题。效果图就不上传了。

作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!

原文地址:https://www.cnblogs.com/linlf03/p/2959164.html