1. 需要激活自定义视图功能
actionBar.setDisplayShowCustomEnabled(true);
/src/com/wind/actionbar/HelloActionBarActivity.java
res/layout/two_button_hor.xml
actionBar.setDisplayShowCustomEnabled(true);
/src/com/wind/actionbar/HelloActionBarActivity.java
package com.wind.actionbar;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class HelloActionBarActivity extends Activity{
private static final String TAG = "HelloActionBarActivity";
private ActionBar actionBar;
private Button mButton;
private LinearLayout mL1;
private LinearLayout mL2;
protected boolean mFlagDial = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate");
setContentView(R.layout.main);
actionBar = getActionBar();
//custom title
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME |
ActionBar.DISPLAY_SHOW_TITLE);
//上句相当于:
/*actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
*/
actionBar.setCustomView(R.layout.two_button_hor);
mButton = (Button) this.findViewById(R.id.button);
mL1 = (LinearLayout) this.findViewById(R.id.action_layout);
mL2 = (LinearLayout) this.findViewById(R.id.action_layout_edit);
mButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(!mFlagDial){
mL1.setVisibility(View.GONE);
mL2.setVisibility(View.VISIBLE);
mFlagDial = true;
}else{
mL1.setVisibility(View.VISIBLE);
mL2.setVisibility(View.GONE);
mFlagDial = false;
}
}
});
}
}
2. 自定义视图res/layout/two_button_hor.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/action_layout"
android:background="@color/green"
android:orientation="horizontal" >
<TextView
android:id="@+id/button_dial"
android:text="Dial"
android:textSize="30sp"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/button_contacts"
android:text="Contacts"
android:textSize="30sp"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/action_layout_edit"
android:visibility="gone"
android:background="@color/blue"
android:orientation="horizontal" >
<EditText
android:id="@+id/edit_number"
android:text="13764749630"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button_del"
android:text="Del"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>