国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 自定義標題欄的實例詳解

2019-10-22 18:25:47
字體:
來源:轉載
供稿:網友

 Android 自定義標題欄的實例詳解

開發 Android APP 經常會用到自定義標題欄,而有多級頁面的情況下還需要給自定義標題欄傳遞數據。

本文要點:

自定義標題填充不完整
自定義標題欄返回按鈕的點擊事件

一、代碼

這里先介紹一下流程:

1. 創建一個標題欄布局文件 mytitlebar.xml
2. 在style.xml中創建 mytitlestyle 主題
3. 創建類 CustomTitleBar
4. 在需要自定義標題欄的Activity的OnCreate方法中實例化 CustomTitleBar
5. 在 AndroidManifest.xml 對使用了自定義標題欄的Activity定義主題

1.定義一個自定義的標題欄布局 mytitlebar.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout  android:id="@+id/re_title" xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="50dp" //定義自定義標題欄的高度  android:background="@color/start_background"  android:orientation="horizontal">  <ImageButton    android:scaleType="fitXY"    android:layout_alignParentLeft="true"    android:layout_centerVertical="true"    android:layout_marginLeft="10dp"    android:id="@+id/bt_back"    android:layout_width="25dp"    android:layout_height="25dp"    android:src="@drawable/left_back"    android:background="@color/touming"/>  <TextView    android:id="@+id/mytitle"    android:layout_centerInParent="true"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:gravity="center"//使文字在整個標題欄的中間    android:textColor="#fff"    android:textSize="20dp" /></RelativeLayout >

2.在 style.xml 中創建 mytitlestyle 主題

<resources>  <!-- 自定義標題欄 parent="android:Theme" 這個屬性必須寫 -->  <style name="mytitlestyle" parent="android:Theme">    <!-- 設置高度,和 mytitlebar.xml中保持一致 -->    <item name="android:windowTitleSize">50dp</item>    <!-- 設置內填充為0 使自定義標題填充整個標題欄,否則左右兩邊有空隙 -->    <item name="android:padding">0dp</item>  </style></resources>

3.創建類 CustomTitleBar

public class CustomTitleBar {  private Activity mActivity;  //不要使用 static 因為有三級頁面返回時會報錯  /**   * @param activity   * @param title   * @see [自定義標題欄]   */  public void getTitleBar(Activity activity, String title) {    mActivity = activity;   activity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);   //指定自定義標題欄的布局文件    activity.setContentView(R.layout.mytitlebar);    activity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,        R.layout.mytitlebar);//獲取自定義標題欄的TextView控件并設置內容為傳遞過來的字符串    TextView textView = (TextView) activity.findViewById(R.id.mytitle);    textView.setText(title);    //設置返回按鈕的點擊事件    ImageButton titleBackBtn = (ImageButton) activity.findViewById(R.id.bt_back);    titleBackBtn.setOnClickListener(new OnClickListener() {      public void onClick(View v) {      //調用系統的返回按鍵的點擊事件        mActivity.onBackPressed();      }    });  }}

4.在需要自定義標題欄的Activity的OnCreate方法中實例化 CustomTitleBar,這里是food頁面

public class food extends Activity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //實例化CustomTitleBar 傳遞相應的參數    CustomTitleBar ct = new CustomTitleBar();    ct.getTitleBar(this, "美食");    setContentView(R.layout.page_food);  }}

5.在 AndroidManifest.xml 對使用了自定義標題欄的Activity定義主題

//省略了其余部分,android:theme="@style/mytitlestyle"這句必需寫<activity      android:name=".food"      android:label="@string/activity_food"      android:theme="@style/mytitlestyle" />

二、總結

使用自定義標題欄的時候,很多人會遇到填充不滿,左右兩邊有空隙以及返回按鈕點擊事件不響應的問題,這里測試和總結了最為合適的方式解決。

自定義標題欄填充不滿,網上有不少解決方案,有的還比較復雜,我這里直接在定義Theme時一個屬性就解決了,還比較容易理解。

自定義標題欄返回按鈕點擊事件不響應或出錯的問題,也是測試了網上的很多代碼,用onBackPressed()最為方便,也有人使用finish(),其余的OnKeyDown之類的測試未通過。

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 图木舒克市| 灵川县| 宁蒗| 紫金县| 滦南县| 广安市| 安远县| 历史| 连平县| 岗巴县| 加查县| 新绛县| 延庆县| 巴楚县| 河源市| 上杭县| 家居| 彭州市| 綦江县| 抚州市| 中卫市| 双峰县| 陈巴尔虎旗| 新野县| 彩票| 通辽市| 来凤县| 江津市| 谢通门县| 沂水县| 枣庄市| 西昌市| 久治县| 遂川县| 新乡市| 安西县| 高密市| 昭平县| 揭阳市| 修文县| 个旧市|