android自定義標題欄

2021-07-02 22:16:20 字數 2083 閱讀 1963

用自定義控制項的方法建立上面的標題欄,順便重溫一下自定義控制項的步驟。

下面說一下整個過程:

1.在layout下新增標題欄布局檔案來設定標題欄樣式

<?xml version="1.0" encoding="utf-8"?>

2.在values資料夾下建立attrs.xml檔案

<?xml version="1.0" encoding="utf-8"?>

這個檔案主要用來定義自定義控制項的屬性和格式,格式有

reference,float,color,dimension,boolean,string,enum,integer,fraction,flag。

標籤下的name屬性一般程自定義控制項的名字。

3.寫乙個自定義控制項類

package com.isaac.tools;

import com.isaac.myactionbar.r;

import android.content.context;

import android.content.res.typedarray;

import android.util.attributeset;

import android.view.layoutinflater;

import android.view.view;

import android.widget.imagebutton;

import android.widget.relativelayout;

import android.widget.textview;

public class myactionbar extends relativelayout

public imagebutton getbuttonleft()

public void setbuttonleft(imagebutton buttonleft)

public imagebutton getbuttonright1()

public void setbuttonright1(imagebutton buttonright1)

public imagebutton getbuttonright2()

public void setbuttonright2(imagebutton buttonright2)

public imagebutton getbuttonright3()

public void setbuttonright3(imagebutton buttonright3)

public textview gettitleview()

public void settitleview(textview titleview)

}

這個自定義控制項類要繼承view類,根據實際情況,這裡繼承relativelayout類。通過typearray給自定義屬性賦值。到這裡,乙個自定義標題欄就已經完成了,下面講怎麼把它新增到activity中。

這裡要注意,為了得到自定義的屬性,要在xml檔案的根布局下新增命名空間xmlns:isaac=""

接下來就可以在activity中得到自定義控制項

package com.isaac.tools;

import android.os.bundle;

import android.view.view;

import android.view.view.onclicklistener;

import android.widget.imagebutton;

import android.widget.toast;

import com.isaac.myactionbar.r;

public class mainactivity extends activity

});}

}

android自定義標題欄

1.建立自定義標題欄的xml檔案,可以給標題欄加背景 引用或者在drawable檔案下通過xml檔案生成 2.在activity中通過requestwindowfeature window.feature custom title 宣告使用自定義標題 3.setcontentview r.layou...

自定義 android 標題欄

1 設計標題欄的樣式 在 value 下新建 styles.xml 檔案,設計標題欄的背景色及大小 2 設計標題欄的布局 在 layout 下新建 custom title.xml 檔案,設計在標題欄左右兩邊分別顯示返回按鈕和選單按鈕 按鈕圖示可以在 阿里巴巴向量圖示庫找到。3 應用自定義的標題欄 ...

Android 自定義標題欄 填滿問題

android 每個activity介面,都會自動生成乙個灰色的標題欄,在編寫程式時,可以選擇是否有標題欄,或者自定義標題欄,自定義標題欄時,可以在標題欄位置,放置乙個布局 系統自帶 img 可以通過 this.requestwindowfeature window.feature custom t...