[英]Custom actionbar layout with overflow menu
我使用帶有自定義操作欄的actionbarsherklock
庫,如下所示:
我的自定義工具:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
// Do any other config to the action bar
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// set custom view
View actionBarView = getLayoutInflater().inflate(
R.layout.action_bar_default, null);
View btnMenuLeft= actionBarView.findViewById(R.id.btnMenuLeft);
btnMenuLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
toggle();
}
});
View btnMenuShare= actionBarView.findViewById(R.id.btnMenuShare);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
actionBar.setCustomView(actionBarView, params);
// Hide the home icon
actionBar.setIcon(android.R.color.transparent);
actionBar.setLogo(android.R.color.transparent);
這是自定義布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/nav_bar_bg"
android:gravity="center"
android:orientation="horizontal" >
<!-- menu button -->
<ImageButton
android:id="@+id/btnMenuLeft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_btn"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false" />
<!-- logo -->
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/app_logo" />
<!-- share button -->
<ImageButton
android:id="@+id/btnMenuShare"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/action_btn"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false" />
問題是我想添加一個溢流菜單來分享像這樣的按鈕:
請告訴我如何使用自定義操作欄布局執行此操作。
似乎沒有right
方法來解決這個問題,所以我嘗試使用帶有列表適配器的popup windows
來假冒操作欄溢出菜單。
它看起來像這樣的例子: http : //rajeshandroiddeveloper.blogspot.com/2013/07/android-popupwindow-example-in-listview.html
只需覆蓋這兩個方法來調用活動中的溢出菜單,其中一個是擴展ActionBarActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.