在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="/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"/>