如何在自定义操作栏中显示主页按钮?

时间:2013-12-16 10:04:25

标签: android android-actionbar

我正在使用NavigationDrawer documentation,但我需要使用costum操作栏。 为了激活抽屉,我需要使用主页按钮,就像我使用costum视图之前一样。使用自定义操作栏时,动作菜单显示良好,但我无法显示主页按钮。我的目标是13和13+ api,所以我使用的代码如下:

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getActionBar().setCustomView(R.layout.actionbar);

actionbar.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@color/blue"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/app_name"
        android:textColor="#ffffff"
        android:textSize="24sp" />
</LinearLayout>

好吧,我可能只是在那里添加一个按钮,但我在这里很懒,并保留该选项作为最后的手段。

更新

当我在没有自定义视图的情况下使用NavigationDrawer时,我会将#1作为操作栏。

ActionBar

当我在自定义视图中使用NavigationDrawer时,我将#2作为操作栏。 我想要的是将#4作为动作栏。

PS:#3未包含在自定义视图中,它是通过onCreateOptionsMenu

创建的

3 个答案:

答案 0 :(得分:0)

您是否在manifest.xml中设置了父级?

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

答案 1 :(得分:0)

解决这个问题的唯一方法是不要懒惰并实现主页按钮(和#3因某种原因)。

actionbar.xml

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

<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:text="@string/app_name"
    android:textColor="#ffffff"
    android:textSize="24sp" />

<ImageView
    android:id="@+id/leftButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:clickable="true"
    android:contentDescription="@string/app_name"
    android:hapticFeedbackEnabled="true" />


<ImageView
    android:id="@+id/rightButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:clickable="true"
    android:contentDescription="@string/app_name"
    android:hapticFeedbackEnabled="true" />

</RelativeLayout>

on活动的onCreate方法

    mLeftButton = findViewById(R.id.leftButton);
    mRightButton = findViewById(R.id.rightButton);
    mLeftButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mSideMenuDrawerLayout.isDrawerOpen(mSideMenuList)) {
                mSideMenuDrawerLayout.closeDrawer(mSideMenuList);
            } else {
                mSideMenuDrawerLayout.openDrawer(mSideMenuList);
            }
        }
    });
    mRightButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Add Button Clicked", Toast.LENGTH_SHORT).show();
        }
    });

PS:任何以前更好的解决方案都将被接受。

答案 2 :(得分:0)

解决方案非常简单) 更改布局

android:layout_width="match_parent"

就此:

android:layout_width="wrap_content"

它会有所帮助,因为你的初始布局占据了所有可见的位置而且没有“家”的地方