分享:Android之自定義標題

咱們知道咱們建立的每個Activity,系統默認爲咱們提供了一下黑色的標題,本篇我將帶領你們接觸一下如何實現自定義標題樣式。相比系統爲咱們提供的樣式,自定義標題能夠知足咱們惟心所欲的自定義設計,使咱們的界面看上去更加的高端上檔次,以便更好的吸引用戶的使用。下面開始今天的內容介紹:java

 

1、既然是自定義標題樣式,首先咱們須要設計一個自定義標題佈局,經過這個佈局文件,咱們能夠爲所欲爲的設計咱們的標題樣式(title.xml):android

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#aa0000"
        android:text="這是個人自定義標題"
        />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更多"
        />
 
</LinearLayout>

2、寫好佈局文件了,下面咱們開始設計標題的樣式,項目res目錄下styles.xmlapp

<resources>
 
    <style name="itcastTheme" parent="android:Theme">
         <item name="android:windowContentOverlay">@color/nonecolor</item>
         <item name="android:windowTitleSize">44dp</item><!-- 設置自定義標題的寬度 -->
         <item name="android:windowTitleBackgroundStyle">@style/itcastbg</item><!-- 自定義標題的樣式 -->
    </style>
   
    <style name="itcastbg">
        <item name="android:background">@drawable/rectangle</item>
    </style>
 
</resources>

 

3、紅色字體部分,是我經過drawable文件下的rectangle.xml文件實現的一個標題背景:ide

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <gradient
        android:angle="270"
        android:endColor="#1DC9CD"
        android:startColor="#A2E0FB" />
    <padding
        android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
</shape>

 

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);//設置咱們自定義標題
        Button  mybutton = (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設置一下樣式:this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.hpu.activity_title"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".MainActivity"
            android:theme="@style/itcastTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

 

好了,關於Android自定義標題的介紹就說完了,固然,開發完APP也是須要進行全方位的檢測:www.ineice.comspa

相關文章
相關標籤/搜索