android自定義窗口標(biāo)題示例分享
1、建好項(xiàng)目之后在它的layout文件夾下創(chuàng)建一個(gè)title.xml文件,作為自定義窗口標(biāo)題的文件。
<?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文件,為窗口應(yīng)用上漸變效果。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充色為漸變色,不需要中間顏色startColor開始和結(jié)束的顏色.-->
<gradient
android:angle="270"
android:endColor="#1DC9CD"
android:startColor="#A2E0FB"/>
<!-- 定義內(nèi)間距 -->
<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后臺(tái)代碼進(jìn)行自定義窗口設(shè)置。
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;
//自定義標(biāo)題
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1.設(shè)置使用自定義窗口
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
// 2.給窗口引入自定義標(biāo)題的xml界面文件
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
}
public void add(View v) {
Toast.makeText(this, "按鈕被點(diǎn)擊", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
5、部署項(xiàng)目,可以顯示自定義的窗口標(biāo)題。可是自定義的窗口標(biāo)題距離界面左右兩端有一點(diǎn)距離,并沒有完全覆蓋。為了解決這一個(gè)問題,需要覆蓋android的窗口標(biāo)題。下面是android窗口標(biāo)題的源碼。
<!--2. 注意: 系統(tǒng)窗口的界面文件在Android系統(tǒng)源代碼android-sdk-windows\platforms\android-8\data\res\layout下的screen_custom_title.xml,內(nèi)容如下:
1.一個(gè)線性布局-->
<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的主題樣式,現(xiàn)在需要繼承重寫它的樣式,代碼如下
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 定義一個(gè)樣式,覆蓋原有主題樣式 -->
<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">自定義標(biāo)題</string>
<drawable name="color">#00000000</drawable>
</resources>
- Android自定義PopupWindow仿點(diǎn)擊彈出分享功能
- 分享Android中Toast的自定義使用
- Android自定義控件之組合控件學(xué)習(xí)筆記分享
- Android自定義控件之開關(guān)按鈕學(xué)習(xí)筆記分享
- Android中制作自定義dialog對(duì)話框的實(shí)例分享
- Android ImageButton自定義按鈕的按下效果的代碼實(shí)現(xiàn)方法分享
- Android中使用ListView繪制自定義表格技巧分享
- Android自定義View設(shè)定到FrameLayout布局中實(shí)現(xiàn)多組件顯示的方法 分享
- Android編程實(shí)現(xiàn)自定義分享列表ACTION_SEND功能的方法
相關(guān)文章
Android Studio與SVN版本控制程序的協(xié)作使用指南
這篇文章主要介紹了Android Studio與SVN版本控制程序的協(xié)作使用指南,使用Gradle插件自動(dòng)填寫SVN號(hào)并發(fā)布到指定目錄的方法,需要的朋友可以參考下2016-03-03GridView基于pulltorefresh實(shí)現(xiàn)下拉刷新 上拉加載更多功能(推薦)
原理和listview一樣 ,都是重寫Android原生控件。下面小編通過實(shí)例代碼給大家分享GridView基于pulltorefresh實(shí)現(xiàn)下拉刷新 上拉加載更多功能,非常不錯(cuò),一起看看吧2016-11-11Android中的廣播(BroadCast)詳細(xì)介紹
這篇文章主要介紹了Android中的廣播(BroadCast)詳細(xì)介紹,本文講解了什么是廣播、廣播有什么用、實(shí)現(xiàn)廣播、動(dòng)態(tài)注冊(cè)方式、配置文件方式等內(nèi)容,需要的朋友可以參考下2015-03-03Android編寫Router路由框架實(shí)例過程詳解
為什么要用路由框架,路由框架哪些好處等等,在此就不做解釋2023-04-04
最常用的框架是ARouter,那是不是可以自己寫一個(gè)路由框架呢,不參考ARouter的方式Android自定義View實(shí)現(xiàn)音頻播放圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)音頻播放圓形進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android dataBinding與ListView及事件詳解
這篇文章主要介紹了Android dataBinding與ListView及事件詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10Android編程實(shí)現(xiàn)自動(dòng)調(diào)整TextView字體大小以適應(yīng)文字長(zhǎng)度的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)自動(dòng)調(diào)整TextView字體大小以適應(yīng)文字長(zhǎng)度的方法,涉及Android基于TextView類的繼承及Paint屬性操作實(shí)現(xiàn)字體大小自適應(yīng)的相關(guān)技巧,需要的朋友可以參考下2016-01-01