具有自定义布局的操作栏中没有HomeUp按钮

时间:2015-08-06 08:06:12

标签: android android-appcompat android-actionbar-compat

我正在尝试在操作栏中添加(homeup)按钮。我在清单中添加了父活动选项,如果我不使用自定义布局,它可以工作。我的自定义布局代码如下。

actionbar = getSupportActionBar();
View view  = getLayoutInflater().inflate(R.layout.top_header_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.FILL_PARENT,ActionBar.LayoutParams.FILL_PARENT);
actionbar.setCustomView(view, params);
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

    <ImageView
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/top_bar_logo"
        />

</FrameLayout>

清单

<activity
   android:name=".MoreDetailsActivity"
   android:configChanges="keyboardHidden|orientation|screenSize"
   android:parentActivityName=".AccountInfoActivity"
   android:label="@string/more_details">

   <meta-data android:name="android.support.PARENT_ACTIVITY"
       android:value=".AccountInfoActivity"/>
</activity>

2 个答案:

答案 0 :(得分:1)

尝试替换

actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);

还可以使用此actionBar.setIcon(your_drawable);来显示您的后退图标。

答案 1 :(得分:0)

做类似的事

活动内部onCreate

actionbar = getSupportActionBar();
View view  = getLayoutInflater().inflate(R.layout.top_header_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.FILL_PARENT,ActionBar.LayoutParams.FILL_PARENT);
actionbar.setCustomView(view, params);
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeButtonEnabled(true);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

现在在onOptionsItemSelected

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == android.R.id.home) {
        //Your code here. In your case use Intent
        return true;
    }

    return super.onOptionsItemSelected(item);
}

如果你想让硬件后退按钮监听器使用这样的东西。

 @Override
public void onBackPressed() {
    super.onBackPressed();
    //Your intent here
}