本文將通過一個實例講解怎么實現在4.0及以上系統版本中實現自定義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中,否側會報錯: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中添加實現代碼
- 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);位置是固定的.
希望讀者朋友們通過這個例子能夠對怎么實現在4.0及以上系統版本中自定義TitleBar有深刻理解和掌握。
本文標題:Android開發如何在4.0及以上系統中自定義TitleBar-創新互聯
鏈接URL:http://m.2m8n56k.cn/article16/dipcdg.html
成都網站建設公司_創新互聯,為您提供網站排名、外貿建站、面包屑導航、服務器托管、標簽優化、App設計
廣告
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源:
創新互聯