日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android開發(fā)如何在4.0及以上系統(tǒng)中自定義TitleBar

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

xml布局文件

activity_main.xml

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

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

 
 
  1.  
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="48dp" 
  4.     android:orientation="horizontal" > 
  5.     
  6.         android:layout_width="wrap_content" 
  7.         android:layout_height="wrap_content" 
  8.         android:layout_gravity="center" 
  9.         android:layout_weight="1.5" 
  10.         android:src="@drawable/ic_action_search" /> 
  11.     
  12.         android:layout_width="wrap_content" 
  13.         android:layout_height="48dp" 
  14.         android:layout_weight="1.5" 
  15.         android:paddingTop="1dp" 
  16.         android:text="@string/app_name" 
  17.         android:textSize="14sp" /> 
  18.     
  19.         android:id="@+id/searchparameter" 
  20.         android:layout_width="wrap_content" 
  21.         android:layout_height="48dp" 
  22.         android:layout_margin="1dp" 
  23.         android:layout_weight="5" 
  24.         android:text="ABCDEFGHIJ" 
  25.         android:textSize="14sp" /> 
  26.     
  27.         android:id="@+id/button" 
  28.         android:layout_width="wrap_content" 
  29.         android:layout_height="48dp" 
  30.         android:layout_margin="1dp" 
  31.         android:layout_weight="2" 
  32.         android:text="OK" 
  33.         android:textSize="14sp" /> 
  34.  

為布局文件修改style.xml

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

 
 
  1.  
  2.  
  3.      
  4.         #047BF0 
  5.      
  6.      
  7.         48dp 
  8.         @style/CustomizedWindowTitleBackground 
  9.          
  10.      
  11.  

在AndroidManifest.xml添加主題樣式

android:theme="@style/titlebarstyle"

 
 
  1.  
  2.     package="com.example.titlebardemo" 
  3.     android:versionCode="1" 
  4.     android:versionName="1.0" > 
  5.     
  6.         android:minSdkVersion="8" 
  7.         android:targetSdkVersion="17" /> 
  8.     
  9.         android:allowBackup="true" 
  10.         android:icon="@drawable/ic_launcher" 
  11.         android:label="@string/app_name" > 
  12.         
  13.             android:name="com.example.titlebardemo.MainActivity" 
  14.             android:label="@string/app_name" 
  15.            android:theme="@style/titlebarstyle" > 
  16.              
  17.                  
  18.                  
  19.              
  20.          
  21.      
  22.  

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

 
 
  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);位置是固定的.

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


文章題目:Android開發(fā)如何在4.0及以上系統(tǒng)中自定義TitleBar
網(wǎng)站鏈接:http://www.dlmjj.cn/article/dhgdjhc.html