如何在android中创建自定义操作栏?

时间:2015-07-10 09:24:02

标签: android menu android-actionbar

我想在android中创建自定义操作栏, 这是我的简单代码:     公共类MainActivity扩展了Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        menubar();
    }

    public void menubar(){

        ActionBar mActionBar = getActionBar();
        LayoutInflater inflater = getLayoutInflater();

        View mCustomView    = inflater.inflate(R.layout.menu_bar, null);
        ImageButton button  = (ImageButton) mCustomView.findViewById(R.id.bt_menu);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Clicked!",Toast.LENGTH_LONG).show();
            }
        });

        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);

    }

}

但是当我像这样运行显示错误时:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setCustomView(android.view.View)' on a null object reference
        at dot.com.coba.MainActivity.menubar(MainActivity.java:39)
        at dot.com.coba.MainActivity.onCreate(MainActivity.java:21)

4 个答案:

答案 0 :(得分:2)

如果要创建自己的操作栏,请隐藏默认操作栏。把这一行放在你的活动

getSupportActionBar().hide();

并在Xml文件中创建自己的布局,就像我在下面创建的那样

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:gravity="center"
        android:orientation="horizontal">


        <ImageButton
            android:id="@+id/ibBack"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:background="@android:color/transparent"
            android:padding="15dp"
            android:src="@drawable/back_1"
            android:visibility="visible" />

        <TextView
            style="?android:textAppearanceLarge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="10sp"
            android:id="@+id/tvTitle"
            android:text="Activity name"
            android:textColor="@color/whiteColor"
            android:textStyle="normal" />
    </RelativeLayout>
</LinearLayout>

*Output of xml Layout*

现在只需将标题设置为您的要求,您就可以轻松地在imagebutton click-listner中调用backpress事件

听起来很简单!哈哈

答案 1 :(得分:1)

首先,请阅读this Android开发者博客文章 请注意,现在,您应该使用Toolbar而不是ActionBar

  

在此版本中,Android引入了一个新的工具栏小部件。 这是一个   动作栏模式的概括,为您提供更多   控制和灵活性。工具栏就是您的层次结构中的视图   任何其他,使其更容易与你的其他人交错   视图,为其设置动画,并对滚动事件做出反应。你也可以设置它   作为Activity的操作栏,意味着您的标准选项菜单   动作将显示在其中。

换句话说,ActionBar现在变成了一种特殊的Toolbar。这是Google's official Material Design spec document的摘录。

  

应用栏(以前称为Android中的操作栏)是一种特殊的工具栏,用于品牌塑造,导航,搜索和操作。

如何在项目中设置Toolbar 1)。build.gradle文件中:

compile 'com.android.support:appcompat-v7:22.2.0'

2)。Activity扩展您的AppCompatActivity

public class MyActivity extends AppCompatActivity{

3)。创建指向Toolbar的{​​{1}} 类成员 Activity或使用ViewHolder模式:

public class MyActivity extends AppCompatActivity{
    //Some code

    private Toolbar mActionBarToolbar;

    //Some code
}

4)。getActionBarToolbar()

中创建新方法MyActivity
protected Toolbar getActionBarToolbar() {
    if (mActionBarToolbar == null) {
        mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        if (mActionBarToolbar != null) {
            setSupportActionBar(mActionBarToolbar);
        }
    }
    return mActionBarToolbar;
}

5)。覆盖setContentView的方法MyActivity

@Override
public void setContentView(int layoutResID) {
    super.setContentView(layoutResID);
    getActionBarToolbar();
}

6)。创建文件res/layout/toolbar_actionbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto"
    myapp:theme="@style/ActionBarThemeOverlay"
    myapp:popupTheme="@style/ActionBarPopupThemeOverlay"
    android:id="@+id/toolbar_actionbar"
    android:background="@null"
    myapp:titleTextAppearance="@style/ActionBar.TitleText"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize" />

设置您的值到属性myapp:thememyapp:popupThememyapp:titleTextAppearance删除

7)。包含在您的活动布局中(对我而言layout_my_activity.xml):

<include layout="@layout/toolbar_actionbar" />

8)。运行您的项目。

答案 2 :(得分:0)

您的getActionBar()方法返回null,因此请尝试getSupportActionBar(),如下所示:

ActionBar mActionBar = getSupportActionBar();

答案 3 :(得分:0)

    mContext = mAppCompatActivity.getBaseContext();
    ActionBar mActionBar = mAppCompatActivity.getSupportActionBar();
    ActionBar.LayoutParams mParams = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT);
    mFrameLayout = new FrameLayout(mContext);
    mFrameLayout.setLayoutParams(mParams);
    if (mActionBar != null) {
        mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        mActionBar.setDisplayShowCustomEnabled(true);
        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setCustomView(mFrameLayout);
        Toolbar mToolbar = (Toolbar) mFrameLayout.getParent();
        mToolbar.setPadding(0, 0, 0, 0);
        mToolbar.setContentInsetsAbsolute(0, 0);
    }