在Activity中加載ActionBar的自定義布局
(1)ActionBar加載自定義布局的代碼封裝如下:
private void setActionBarLayout(int layoutId) {
// TODO Auto-generated method stub
ActionBar actionBar = getActionBar();
if(null != actionBar){
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(layoutId, null);
ActionBar.LayoutParams lParams = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
actionBar.setCustomView(v, lParams);
}
}
(2)在onCreate()方法中調用該方法,并傳入自定義的布局檔案ID:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setActionBarLayout(R.layout.actionbar_layout);
}
(3)自定義布局檔案“actionbar_port_layout.xml ”代碼如下:
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="44dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#008000">
android:layout_marginLeft="24dp"
android:layout_width="36dp"
android:layout_height="36dp"
android:src="@drawable/ic_launcher"/>
android:layout_marginLeft="24dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="36dp"
android:hint="Please input "
android:id="@+id/et_input"/>
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/action_search"/>