android自訂視窗標題樣本分享_Android

來源:互聯網
上載者:User

1、建好項目之後在它的layout檔案夾下建立一個title.xml檔案,作為自訂視窗標題的檔案。

複製代碼 代碼如下:

<?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="match_parent"
        android:text="@string/hello_world"
        android:textColor="#FF00FF"
         />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="add"
        android:text="添加" />
</LinearLayout>

2、在res/drawable檔案下建立rectangle.xml檔案,為視窗應用上漸層效果。

複製代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
   <!--  填充色為漸層色,不需要中間顏色startColor開始和結束的顏色.-->
    <gradient
        android:angle="270"     
        android:endColor="#1DC9CD"
        android:startColor="#A2E0FB"/>
    <!-- 定義內間距 -->
    <padding
        android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
</shape>

3、布局檔案:

複製代碼 代碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />
</RelativeLayout>

4、通過activity後台代碼進行自訂視窗設定。

複製代碼 代碼如下:

package com.example.customertitle;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

//自訂標題
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 1.設定使用自訂視窗
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        // 2.給視窗引入自訂標題的xml介面檔案
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
    }

    public void add(View v) {
        Toast.makeText(this, "按鈕被點擊", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

5、部署項目,可以顯示自訂的視窗標題。可是自訂的視窗標題距離介面左右兩端有一點距離,並沒有完全覆蓋。為瞭解決這一個問題,需要覆蓋android的視窗標題。下面是android視窗標題的源碼。

複製代碼 代碼如下:

<!--2. 注意: 系統視窗的介面檔案在Android系統原始碼android-sdk-windows\platforms\android-8\data\res\layout下的screen_custom_title.xml,內容如下:
           1.一個線性布局-->
 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <FrameLayout android:id="@android:id/title_container"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/windowTitleSize"
        style="?android:attr/windowTitleBackgroundStyle">
    </FrameLayout>
    <FrameLayout android:id="@android:id/content"
        android:layout_width="match_parent"
        android:layout_height="0dip"
         android:layout_weight="1"
        android:foregroundGravity="fill_horizontal|top"
        android:foreground="?android:attr/windowContentOverlay" />
 </LinearLayout>

android:attr/windowTitleSize
android:attr/windowTitleBackgroundStyle
android:attr/windowContentOverlay

上述屬性的值在android-sdk-windows\platforms\android-8\data\res\values下的themes.xml檔案中定義:

複製代碼 代碼如下:

   <style name="Theme">
       <itemname="windowContentOverlay">@android:drawable/title_bar_shadow</item>
        <itemname="windowTitleSize">25dip</item>
       <itemname="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
   </style>
@android:style/WindowTitleBackground樣式在android-sdk-windows\platforms\android-8\data\res\values下的styles.xml檔案中定義:
   <style name="WindowTitleBackground">
        <itemname="android:background">@android:drawable/title_bar</item>
   </style>


通過上述可以知道android的主題樣式,現在需要繼承重寫它的樣式,代碼如下

複製代碼 代碼如下:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 定義一個樣式,覆蓋原有主題樣式  -->
    <style name="myTheme" parent="android:Theme">
        <item name="android:windowContentOverlay">@drawable/color</item>
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/textViewBg</item>
    </style>

    <style name="textViewBg">
        <item name="android:background">@drawable/rectangle</item>
    </style>
</resources>

顏色值的定義

複製代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">CustomerTitle</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">自訂標題</string>
    <drawable name="color">#00000000</drawable>
</resources>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.