Android開(kāi)發(fā)怎么在4.0及以上系統(tǒng)中自定義TitleBar

這篇文章主要講解了“Android開(kāi)發(fā)怎么在4.0及以上系統(tǒng)中自定義TitleBar”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Android開(kāi)發(fā)怎么在4.0及以上系統(tǒng)中自定義TitleBar”吧!

創(chuàng)新互聯(lián)是專(zhuān)業(yè)的康巴什網(wǎng)站建設(shè)公司,康巴什接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行康巴什網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!

本文將通過(guò)一個(gè)實(shí)例講解怎么實(shí)現(xiàn)在4.0及以上系統(tǒng)版本中實(shí)現(xiàn)自定義TitleBar,這只是我自己找到的一種方法;

xml布局文件

activity_main.xml

<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" >      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/hello_world" /> </RelativeLayout>

自定義的Titlebar的布局文件titlebar.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="48dp"     android:orientation="horizontal" >     <ImageView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_weight="1.5"         android:src="@drawable/ic_action_search" />     <TextView         android:layout_width="wrap_content"         android:layout_height="48dp"         android:layout_weight="1.5"         android:paddingTop="1dp"         android:text="@string/app_name"         android:textSize="14sp" />     <EditText         android:id="@+id/searchparameter"         android:layout_width="wrap_content"         android:layout_height="48dp"         android:layout_margin="1dp"         android:layout_weight="5"         android:text="ABCDEFGHIJ"         android:textSize="14sp" />     <Button         android:id="@+id/button"         android:layout_width="wrap_content"         android:layout_height="48dp"         android:layout_margin="1dp"         android:layout_weight="2"         android:text="OK"         android:textSize="14sp" /> </LinearLayout>

為布局文件修改style.xml

此處的style.xml在values-11或者value-14中,否側(cè)會(huì)報(bào)錯(cuò):you cannot combine custom titles with other title features

<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android">     <style name="CustomizedWindowTitleBackground">         <item name="android:background">#047BF0</item>     </style>     <style name="titlebarstyle" parent="android:Theme">         <item name="android:windowTitleSize">48dp</item>         <item name="android:windowTitleBackgroundStyle">@style/CustomizedWindowTitleBackground</item>         <!-- All customizations that are NOT specific to a particular API-level can go here. -->     </style> </resources>

在AndroidManifest.xml添加主題樣式

android:theme="@style/titlebarstyle"

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

MainActivity.java中添加實(shí)現(xiàn)代碼

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.titlebar);     }

代碼中requestWindowFeature(Window.FEATURECUSTOMTITLE);和getWindow().setFeatureInt(Window.FEATURECUSTOMTITLE,
R.layout.titlebar);位置是固定的.

希望讀者朋友們通過(guò)這個(gè)例子能夠?qū)υ趺磳?shí)現(xiàn)在4.0及以上系統(tǒng)版本中自定義TitleBar有深刻理解和掌握。

感謝各位的閱讀,以上就是“Android開(kāi)發(fā)怎么在4.0及以上系統(tǒng)中自定義TitleBar”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Android開(kāi)發(fā)怎么在4.0及以上系統(tǒng)中自定義TitleBar這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

本文名稱(chēng):Android開(kāi)發(fā)怎么在4.0及以上系統(tǒng)中自定義TitleBar
文章URL:http://muchs.cn/article2/iehgoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站排名、網(wǎng)站營(yíng)銷(xiāo)、關(guān)鍵詞優(yōu)化、網(wǎng)頁(yè)設(shè)計(jì)公司、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)