我們知道我們創建的每一個Activity,系統默認為我們提供了一下黑色的標題,本篇我將帶領大家接觸一下如何實現自定義標題樣式。相比系統為我們提供的樣式,自定義標題可以滿足我們唯心所欲的自定義設計,使我們的界面看上去更加的高端上檔次,以便更好的吸引用戶的使用。下面開始今天的內容介紹:
1、既然是自定義標題樣式,首先我們需要設計一個自定義標題布局,通過這個布局文件,我們可以隨心所欲的設計我們的標題樣式(title.xml):
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#aa0000"
android:text="這是我的自定義標題"
/>
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更多"
/>
2、寫好布局文件了,下面我們開始設計標題的樣式,項目res目錄下styles.xml:
@color/nonecolor
44dp
@style/itcastbg
@drawable/rectangle
3、紅色字體部分,是我通過drawable文件下的rectangle.xml文件實現的一個標題背景:
encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
android:angle="270"
android:endColor="#1DC9CD"
android:startColor="#A2E0FB" />
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
4、到這里我么就可以開始修改我們的主Activity:
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title);//設置我們自定義標題
Buttonmybutton =(Button)findViewById(R.id.button);
mybutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "盡請期待",
Toast.LENGTH_SHORT).show();
}
});
}
}
需要注意的是紅色部分必須寫在引用布局文件之前,不然達不到效果。
5、最后我們需要在AndroidManifest.xml文件中,為我們的Activity設置一下樣式:
xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.hpu.activity_title"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="21" />
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
android:name=".MainActivity"
android:theme="@style/itcastTheme">
好了,關于Android自定義標題的介紹就說完了,當然,開發完APP也是需要進行全方位的檢測:www.ineice.com