gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:34:19 25 4
gpt4 key购买 nike

我想在 android 中创建自定义操作栏,这是我的简单代码:

public class MainActivity extends 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)

最佳答案

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

In this release, Android introduces a new Toolbar widget. This is a generalization of the Action Bar pattern that gives you much more control and flexibility. Toolbar is a view in your hierarchy just like any other, making it easier to interleave with the rest of your views, animate it, and react to scroll events. You can also set it as your Activity’s action bar, meaning that your standard options menu actions will be display within it.

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

The app bar, formerly known as the action bar in Android, is a special kind of toolbar that’s used for branding, navigation, search, and actions.


如何在项目中设置工具栏
1). 在您的 build.gradle 文件中:

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

2).AppCompatActivity 扩展您的 Activity:

public class MyActivity extends AppCompatActivity{

3). 作为 Activityclass member 或使用 ViewHolder 创建指向您的 Toolbar 的链接 模式:

public class MyActivity extends AppCompatActivity{
//Some code

private Toolbar mActionBarToolbar;

//Some code
}

4).MyActivity

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

5). 覆盖 MyActivitysetContentView 方法:

@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 或 < strong>删除它。

7). 包含在您的 Activity 布局中(对我来说是layout_my_activity.xml):

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

8). 运行您的项目。

关于android - 如何在 android 中创建自定义操作栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31337138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com