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

xml布局文件

專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、
外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)
威遠(yuǎn)免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
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>
在A(yíng)ndroidManifest.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有深刻理解和掌握。
本文標(biāo)題:Android開(kāi)發(fā)如何在4.0及以上系統(tǒng)中自定義TitleBar-創(chuàng)新互聯(lián)
鏈接URL:http://vcdvsql.cn/article16/dipcdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、外貿(mào)建站、面包屑導(dǎo)航、服務(wù)器托管、標(biāo)簽優(yōu)化、App設(shè)計(jì)
廣告
聲明:本網(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)