如何更改应用程序中的默认工具栏?
How can I change the default toolbar in my App?
目前,我正在开发一个应用程序,但我是初学者。我添加了一些 activity 布局,但每次启动应用程序时都会出现默认工具栏。如何在没有 "destroying" 实现的布局的情况下自定义此工具栏?
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:elevation="4dp"
app:popupTheme="@style/AppTheme.PopupOverlay">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_logo"
android:padding="20dp"
android:foregroundGravity="center"/>
</android.support.v7.widget.Toolbar>
MainActivity.java(扩展 RxAppCompatActivity)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Toolbar toolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
}
工具栏变量始终为空 - 为什么?
在 activity XML 文件中添加 XML 代码。由于标签不包含在您的 activity_main.xml 中,当您尝试通过 id 查找它时,您将始终得到 NullPointerException。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.screens.MainActivity">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:elevation="4dp"
app:popupTheme="@style/AppTheme.PopupOverlay">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_logo"
android:padding="20dp"
android:foregroundGravity="center"/>
</android.support.v7.widget.Toolbar>
<!--Rest of your XML-->
</androidx.constraintlayout.widget.ConstraintLayout>
注意:我使用 ConstraintLayout 只是为了举例,你可以使用任何你喜欢的布局(尽管 ConstraintLayout 非常棒!)
顺便说一句,由于您使用的是 ButterKnife,因此您可以轻松地使用 @BindView(R.id.my_toolbar) 绑定视图,而不是使用 findViewById()。
目前,我正在开发一个应用程序,但我是初学者。我添加了一些 activity 布局,但每次启动应用程序时都会出现默认工具栏。如何在没有 "destroying" 实现的布局的情况下自定义此工具栏?
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:elevation="4dp"
app:popupTheme="@style/AppTheme.PopupOverlay">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_logo"
android:padding="20dp"
android:foregroundGravity="center"/>
</android.support.v7.widget.Toolbar>
MainActivity.java(扩展 RxAppCompatActivity)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Toolbar toolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
}
工具栏变量始终为空 - 为什么?
在 activity XML 文件中添加 XML 代码。由于标签不包含在您的 activity_main.xml 中,当您尝试通过 id 查找它时,您将始终得到 NullPointerException。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.screens.MainActivity">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:elevation="4dp"
app:popupTheme="@style/AppTheme.PopupOverlay">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_logo"
android:padding="20dp"
android:foregroundGravity="center"/>
</android.support.v7.widget.Toolbar>
<!--Rest of your XML-->
</androidx.constraintlayout.widget.ConstraintLayout>
注意:我使用 ConstraintLayout 只是为了举例,你可以使用任何你喜欢的布局(尽管 ConstraintLayout 非常棒!)
顺便说一句,由于您使用的是 ButterKnife,因此您可以轻松地使用 @BindView(R.id.my_toolbar) 绑定视图,而不是使用 findViewById()。