我正在尝试添加自定义工具栏
这是我的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/toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:subtitleTextColor="@color/sub_text_color"
app:navigationContentDescription="@string/abc_action_bar_up_description"
android:background="@color/sub_text_color"
app:navigationIcon="?attr/homeAsUpIndicator"
/>
这是我的风格
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
我已将其包含在我的活动主体中
<RelativeLayout 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"
tools:context="com.example.myproj.activities.RegisterActivity">
<include layout="@layout/toolbar"/>
在我的主要活动课上,我打电话给
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(R.string.create_account);
}
但是我仍然看不到我班上的工具栏。有人可以帮我找出错误吗
造成此问题的原因很多。
首先,放置在自定义工具栏之后的视图可能会叠加在其上,从而使其无法显示。请记住您正在使用RelativeLayout作为您的根元素。因此,您将要确保工具栏后面的视图具有android:layout_below="@+id/toolbar"
属性。
其次,我建议您使用协调器布局作为rootview,并将自定义工具栏包装在AppBarLayout视图中。这样您的布局文件类似于:
<android.support.design.widget.CoordinatorLayout 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"
tools:context="ng.shoppi.lafarge_app.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include
android:id="@+id/content_main"
layout="@layout/content_main" />
这将使工具栏保持顶部,并确保其他视图位于工具栏下方。祝好运。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句