android自定義窗口標題示例分享
1、建好項目之后在它的layout文件夾下創(chuàng)建一個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開始和結(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后臺代碼進行自定義窗口設置。
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、部署項目,可以顯示自定義的窗口標題??墒亲远x的窗口標題距離界面左右兩端有一點距離,并沒有完全覆蓋。為了解決這一個問題,需要覆蓋android的窗口標題。下面是android窗口標題的源碼。
<!--2. 注意: 系統(tǒng)窗口的界面文件在Android系統(tǒng)源代碼android-sdk-windows\platforms\android-8\data\res\layout下的screen_custom_title.xml,內(nèi)容如下:
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的主題樣式,現(xiàn)在需要繼承重寫它的樣式,代碼如下
<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>
相關(guān)文章
Android Studio與SVN版本控制程序的協(xié)作使用指南
這篇文章主要介紹了Android Studio與SVN版本控制程序的協(xié)作使用指南,使用Gradle插件自動填寫SVN號并發(fā)布到指定目錄的方法,需要的朋友可以參考下2016-03-03GridView基于pulltorefresh實現(xiàn)下拉刷新 上拉加載更多功能(推薦)
原理和listview一樣 ,都是重寫Android原生控件。下面小編通過實例代碼給大家分享GridView基于pulltorefresh實現(xiàn)下拉刷新 上拉加載更多功能,非常不錯,一起看看吧2016-11-11Android自定義View實現(xiàn)音頻播放圓形進度條
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)音頻播放圓形進度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Android dataBinding與ListView及事件詳解
這篇文章主要介紹了Android dataBinding與ListView及事件詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10Android編程實現(xiàn)自動調(diào)整TextView字體大小以適應文字長度的方法
這篇文章主要介紹了Android編程實現(xiàn)自動調(diào)整TextView字體大小以適應文字長度的方法,涉及Android基于TextView類的繼承及Paint屬性操作實現(xiàn)字體大小自適應的相關(guān)技巧,需要的朋友可以參考下2016-01-01