Android UI(下)

7.4         自定义样式

        样式,简单的来说,是View的一组属性,通过这个属性可以方便的定义控件的样式。接下来,我会具体的通过例子来描述如和使用自定义样式。

        首先,样式,我们可以知道,它包括了诸如字体、文字颜色等设置。不过,这些只是改变了View的一些属性,具体的属性还是得依靠所绘制的View。

       

        使用方式:

        1.  定义样式文件

        2.  在view样式中引用样式

       

       

      7.4.1 定义样式文件

            样式文件存放位置为res/values/styles.xml

    <stylename="textTheme">

        <itemname="android:textColor">#F00</item>

        <itemname="android:textSize">20sp</item>

    </style>

代码片段7.4.1.1 样式文件定义

           

           

      7.4.2 在View中使用样式

            引用

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

style="@style/textTheme"

android:text="@string/hello_world"/>

代码片段7.4.2.1 View中引用样式

 

 

      7.4.3 样式的继承

            样式除了直接使用,也可以当作父类被其他样式继承,如下:

    <stylename="textTheme">

        <itemname="android:textColor">#F00</item>

        <itemname="android:textSize">20sp</item>

    </style>

   

    <stylename="textThemeDetail"parent="textTheme">

        <itemname="android:padding">20dp</item>

    </style>

代码片段7.4.3.1 父类样式

           

注释:可以看到,textThemeDetail样式,继承了textTheme的样式,如果想要使用颜色为红色,字体大小为20,内边框距离为20的样式,可以引用textThemeDetail的样式。

       

       

       

      7.4.4 作为主题处理

样式除了作为View的具体属性外,还可以作为主题进行处理。其实,它的本质就是应用于整个屏幕的样式

 

比如,我们定义如下的主题(全屏样式):

<stylename="AppMyTheme">

        <itemname="android:textSize">15sp</item>

</style>

代码片段7.4.4.1 主题样式

 

那么定义了主题后,如何使用这个主题?

我们有两种方法:

1.  在onCreate方法中直接调用java使用

package com.example.androidstudy_style;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

 

public class MainActivity extends Activity {

 

   @Override

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setTheme(R.style.AppMyTheme);

      setContentView(R.layout.activity_main);

   }

 

   @Override

   public boolean onCreateOptionsMenu(Menu menu) {

      // Inflate the menu; thisadds items to the action bar if it is present.

      getMenuInflater().inflate(R.menu.main, menu);

      return true;

   }

 

}

代码片段7.4.4.2 java应用主题

 

 

 

 

除了这种方法,当然还有一种,用xml应用:

<?xmlversion="1.0"encoding="utf-8"?>

<manifestxmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.androidstudy_style"

    android:versionCode="1"

    android:versionName="1.0">

 

    <uses-sdk

       android:minSdkVersion="8"

       android:targetSdkVersion="17"/>

 

    <application

       android:allowBackup="true"

       android:icon="@drawable/ic_launcher"

       android:label="@string/app_name"

       android:theme="@style/AppTheme">

        <activity

           android:name="com.example.androidstudy_style.MainActivity"

           android:theme="@style/AppMyTheme"

           android:label="@string/app_name">

            <intent-filter>

                <actionandroid:name="android.intent.action.MAIN"/>

 

                <categoryandroid:name="android.intent.category.LAUNCHER"/>

            </intent-filter>

        </activity>

    </application>

 

</manifest>

代码片段7.4.4.3 xml应用主题

 

注释:这个的应用实在Android Manifest配置文件中应用,而不是像使用View中的style一样是在activity布局文件中配置。这个是需要注意的地方。

 

另:xml中只能设置一个主题,而java代码中,你却可以通过setTheme的方法设置多个主题来使用。

 

注意:所有主题的调用,都必须在setContentView方法或inflate方法之前被应用。

 

     7.5         小结

        通过Android的各种控件和样式,可以构建出各种各样的应用程序。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章