成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Android開發如何在4.0及以上系統中自定義TitleBar

移動開發 Android
有時候,我們希望能改變一下這樣單調的狀況。比如,要在標題欄中增加一個用于美化界面的圖標、增一個輸入框或按鈕之類的,怎樣才能做到這一點呢?我們不妨來看一個自定義TitleBar的實際例子。

本文將通過一個實例講解怎么實現在4.0及以上系統版本中實現自定義TitleBar,這只是我自己找到的一種方法;

xml布局文件

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:paddingBottom="@dimen/activity_vertical_margin" 
  6.     android:paddingLeft="@dimen/activity_horizontal_margin" 
  7.     android:paddingRight="@dimen/activity_horizontal_margin" 
  8.     android:paddingTop="@dimen/activity_vertical_margin" 
  9.     tools:context=".MainActivity" > 
  10.  
  11.     <TextView 
  12.         android:layout_width="wrap_content" 
  13.         android:layout_height="wrap_content" 
  14.         android:text="@string/hello_world" /> 
  15. </RelativeLayout> 

自定義的Titlebar的布局文件titlebar.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="48dp" 
  5.     android:orientation="horizontal" > 
  6.     <ImageView 
  7.         android:layout_width="wrap_content" 
  8.         android:layout_height="wrap_content" 
  9.         android:layout_gravity="center" 
  10.         android:layout_weight="1.5" 
  11.         android:src="@drawable/ic_action_search" /> 
  12.     <TextView 
  13.         android:layout_width="wrap_content" 
  14.         android:layout_height="48dp" 
  15.         android:layout_weight="1.5" 
  16.         android:paddingTop="1dp" 
  17.         android:text="@string/app_name" 
  18.         android:textSize="14sp" /> 
  19.     <EditText 
  20.         android:id="@+id/searchparameter" 
  21.         android:layout_width="wrap_content" 
  22.         android:layout_height="48dp" 
  23.         android:layout_margin="1dp" 
  24.         android:layout_weight="5" 
  25.         android:text="ABCDEFGHIJ" 
  26.         android:textSize="14sp" /> 
  27.     <Button 
  28.         android:id="@+id/button" 
  29.         android:layout_width="wrap_content" 
  30.         android:layout_height="48dp" 
  31.         android:layout_margin="1dp" 
  32.         android:layout_weight="2" 
  33.         android:text="OK" 
  34.         android:textSize="14sp" /> 
  35. </LinearLayout> 

為布局文件修改style.xml

此處的style.xml在values-11或者value-14中,否側會報錯:you cannot combine custom titles with other title features

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

在AndroidManifest.xml添加主題樣式

android:theme="@style/titlebarstyle"

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     package="com.example.titlebardemo" 
  4.     android:versionCode="1" 
  5.     android:versionName="1.0" > 
  6.     <uses-sdk 
  7.         android:minSdkVersion="8" 
  8.         android:targetSdkVersion="17" /> 
  9.     <application 
  10.         android:allowBackup="true" 
  11.         android:icon="@drawable/ic_launcher" 
  12.         android:label="@string/app_name" > 
  13.         <activity 
  14.             android:name="com.example.titlebardemo.MainActivity" 
  15.             android:label="@string/app_name" 
  16.            android:theme="@style/titlebarstyle" > 
  17.             <intent-filter> 
  18.                 <action android:name="android.intent.action.MAIN" /> 
  19.                 <category android:name="android.intent.category.LAUNCHER" /> 
  20.             </intent-filter> 
  21.         </activity> 
  22.     </application> 
  23. </manifest> 

MainActivity.java中添加實現代碼

  1. protected void onCreate(Bundle savedInstanceState) { 
  2.         super.onCreate(savedInstanceState); 
  3.         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
  4.         setContentView(R.layout.activity_main); 
  5.         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
  6.                 R.layout.titlebar); 
  7.     } 

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

希望讀者朋友們通過這個例子能夠對怎么實現在4.0及以上系統版本中自定義TitleBar有深刻理解和掌握。

責任編輯:閆佳明 來源: my.eoe.cn
相關推薦

2019-12-02 21:29:45

Keras神經網絡TensorFlow

2021-07-01 11:07:49

Swift 自定義操作符

2009-07-03 18:20:45

VSTS 2010網絡

2021-07-16 11:00:40

Django用戶模型Python

2012-05-18 10:52:20

TitaniumAndroid模塊自定義View模塊

2010-02-07 14:02:16

Android 界面

2013-05-02 14:08:18

2017-06-20 12:48:55

React Nativ自定義模塊Note.js

2016-12-26 15:25:59

Android自定義View

2016-11-16 21:55:55

源碼分析自定義view androi

2009-06-08 20:13:36

Eclipse自定義控

2013-05-20 17:48:20

2024-05-08 17:05:44

2019-06-21 09:50:47

Windows 10自定義分辨率

2011-08-09 17:16:56

CoreAnimati動畫

2022-07-04 10:39:24

TienChin項目自定義

2013-06-27 11:10:01

iOS開發自定義UISlider

2013-04-01 14:35:10

Android開發Android自定義x

2021-11-23 15:06:42

Kubernetes 運維開源

2013-05-20 17:33:44

Android游戲開發自定義View
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91精品国产欧美一区二区 | 国产成人黄色 | 久久国产激情视频 | 五月激情婷婷在线 | 伊人久久伊人 | 高清国产午夜精品久久久久久 | 免费在线黄 | 日韩手机在线视频 | 国产毛片av| av手机免费在线观看 | 精品一区二区视频 | 国产视频一区在线 | 精品一区二区久久久久久久网站 | 久久久久中文字幕 | 久久综合九色综合欧美狠狠 | 国产精品区二区三区日本 | 天天看天天操 | 国产成人免费一区二区60岁 | 久久久久久久久久影视 | 在线免费观看黄色网址 | 在线观看av网站 | 三级在线视频 | 免费99精品国产自在在线 | 国产精品久久久久久久久久久久久 | 伦理午夜电影免费观看 | 亚洲视频三区 | 亚洲人va欧美va人人爽 | 国产精品www | 色视频www在线播放国产人成 | 91素人| 亚洲精品国产综合区久久久久久久 | 亚洲精品成人在线 | 国产在线h | 伊人精品一区二区三区 | 麻豆av电影网 | 欧美综合一区二区三区 | 亚洲人在线 | 一区二区三区在线 | 最新中文字幕第一页视频 | 狠狠躁天天躁夜夜躁婷婷老牛影视 | 久久久久久国产精品 |