新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android開發(fā)如何在4.0及以上系統(tǒng)中自定義TitleBar
本文將通過一個實例講解怎么實現(xiàn)在4.0及以上系統(tǒng)版本中實現(xiàn)自定義TitleBar,這只是我自己找到的一種方法;

xml布局文件
activity_main.xml
- 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" >
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
自定義的Titlebar的布局文件titlebar.xml
- android:layout_width="match_parent"
- android:layout_height="48dp"
- android:orientation="horizontal" >
- 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" />
- 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" />
- 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" />
為布局文件修改style.xml
此處的style.xml在values-11或者value-14中,否側(cè)會報錯:you cannot combine custom titles with other title features
在AndroidManifest.xml添加主題樣式
android:theme="@style/titlebarstyle"
- package="com.example.titlebardemo"
- android:versionCode="1"
- android:versionName="1.0" >
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- android:name="com.example.titlebardemo.MainActivity"
- android:label="@string/app_name"
- android:theme="@style/titlebarstyle" >
MainActivity.java中添加實現(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);位置是固定的.
希望讀者朋友們通過這個例子能夠?qū)υ趺磳崿F(xiàn)在4.0及以上系統(tǒng)版本中自定義TitleBar有深刻理解和掌握。
網(wǎng)站欄目:Android開發(fā)如何在4.0及以上系統(tǒng)中自定義TitleBar
文章路徑:http://m.5511xx.com/article/dhgdjhc.html


咨詢
建站咨詢
