如何在 android 中创建自定义操作栏?
How to create custom actionbar in android?
我想在 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)
您的 getActionBar()
方法 returns 无效,因此尝试使用 getSupportActionBar()
像这样:
ActionBar mActionBar = getSupportActionBar();
首先,请阅读 this Android 开发者博客 post。
请注意,现在您应该使用 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.
如何在您的项目中设置 Toolbar
?
1). 在您的 build.gradle
文件中:
compile 'com.android.support:appcompat-v7:22.2.0'
2). 从 AppCompatActivity
:
扩展你的 Activity
public class MyActivity extends AppCompatActivity{
3). 为您的 Toolbar
创建 link 作为 class 成员 Activity
或使用 ViewHolder
模式:
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). 覆盖 MyActivity
的方法 setContentView
:
@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:theme
、myapp:popupTheme
、myapp:titleTextAppearance
或删除它。
7). 包括在你的布局中 activity(对我来说 layout_my_activity.xml
):
<include layout="@layout/toolbar_actionbar" />
8). 运行 你的项目。
如果您想创建自己的操作栏,请隐藏默认操作栏。把这一行放在你的 Activity
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>
就是这样,只需将标题设置为您的要求,您就可以轻松地在图像按钮 click-listner 中调用 backpress 事件
听起来很简单!哈哈
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);
}
我想在 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)
您的 getActionBar()
方法 returns 无效,因此尝试使用 getSupportActionBar()
像这样:
ActionBar mActionBar = getSupportActionBar();
首先,请阅读 this Android 开发者博客 post。
请注意,现在您应该使用 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.
如何在您的项目中设置 Toolbar
?
1). 在您的 build.gradle
文件中:
compile 'com.android.support:appcompat-v7:22.2.0'
2). 从 AppCompatActivity
:
Activity
public class MyActivity extends AppCompatActivity{
3). 为您的 Toolbar
创建 link 作为 class 成员 Activity
或使用 ViewHolder
模式:
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). 覆盖 MyActivity
的方法 setContentView
:
@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:theme
、myapp:popupTheme
、myapp:titleTextAppearance
或删除它。
7). 包括在你的布局中 activity(对我来说 layout_my_activity.xml
):
<include layout="@layout/toolbar_actionbar" />
8). 运行 你的项目。
如果您想创建自己的操作栏,请隐藏默认操作栏。把这一行放在你的 Activity
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>
就是这样,只需将标题设置为您的要求,您就可以轻松地在图像按钮 click-listner 中调用 backpress 事件
听起来很简单!哈哈
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);
}