android - 工具栏在 android 活动上显示为空白
问题描述
我的活动顶部的工具栏显示为空白,但它应该有一个导航自定义图标和一个标题,但两者都不可见?
这是我的xml
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
这是我的活动
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_me)
setupToolbar()
}
private fun setupToolbar() {
setSupportActionBar(toolbar)
toolbar.setNavigationIcon(R.drawable.ic_close)
toolbar.setNavigationOnClickListener {
onBackPressed()
}
toolbar.setTitle(R.string.profile_unfollow_warning)
supportActionBar!!.setHomeButtonEnabled(true)
supportActionBar!!.setDisplayShowCustomEnabled(true)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
supportActionBar!!.show()
}
}
我用于此活动的主题
<activity android:name=".ui.profile.MyActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/Theme.MaterialComponents.NoActionBar"/>
解决方案
public class MainActivity {
// Declaring the Toolbar Object
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// Attaching the layout to the toolbar object
toolbar = (Toolbar) findViewById(R.id.tool_bar);
// Setting toolbar as the ActionBar with setSupportActionBar() call
setSupportActionBar(toolbar);
}
创建一个像 tool_bar.xml 这样的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="4dp" />
将工具栏包含到您的 main_activity.xml 中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
<TextView
android:layout_below="@+id/tool_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/TextDimTop"
android:text="@string/hello_world" />
</RelativeLayout>
推荐阅读
- mysql - Mysql - 按月分组,具有唯一订阅用户。用户不应重复
- elixir - 在 Ecto 中,在带有连接的查询中使用查询函数
- asp.net-mvc - 在 somee.com 上部署数据库以及如何更新 web.config(已发布代码)中的连接字符串后,当我使用实体框架时?
- javascript - 如何为不同的浏览器大小渲染不同的 ejs 文件
- google-earth-engine - 缺少功能“1_1_1_1_0_0”的属性“pc2”
- java - 从 onActivityResult() 修改全局变量会使应用程序崩溃
- sql - SQL如何将数字四舍五入为三?
- c++ - 如何在multimap c ++中插入一个键和多个值
- sql - SQL 将重复项收集到一个地方?PostgreSQL
- apache-kafka - 如果我在代理级别和主题级别设置配置,哪个优先?