工具栏背景不';t调整,不显示标题-android
我正在尝试创建一个工具栏,它使用我在png文件中创建的背景。 现在,当我在xml中使用背景时,android studio中的布局编辑器正好显示了预期的结果。当我在虚拟设备中运行应用程序时,问题就出现了。背景不适应工具栏,因此只显示部分背景 此外,标题和项目表现得很奇怪。标题根本不显示,工具栏中显示的唯一项目突然跳到左侧 工具栏的XML代码:工具栏背景不';t调整,不显示标题-android,android,android-actionbar,toolbar,Android,Android Actionbar,Toolbar,我正在尝试创建一个工具栏,它使用我在png文件中创建的背景。 现在,当我在xml中使用背景时,android studio中的布局编辑器正好显示了预期的结果。当我在虚拟设备中运行应用程序时,问题就出现了。背景不适应工具栏,因此只显示部分背景 此外,标题和项目表现得很奇怪。标题根本不显示,工具栏中显示的唯一项目突然跳到左侧 工具栏的XML代码: <?xml version="1.0" encoding="utf-8"?> <android.support.design.widge
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app_bar_styled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:elevation="15dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_styled"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="15dp"
android:theme="@style/AppTheme.AppBarOverlay.Styled">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/action_bar_background"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
以下是布局编辑器预览和虚拟设备的图像:
布局编辑器:
虚拟设备:您需要像这样嵌套它:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cor_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="256dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_styled"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="15dp"
android:theme="@style/AppTheme.AppBarOverlay.Styled">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/action_bar_background"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
toolbar= (Toolbar) findViewById(R.id.toolbar);
collapse=(CollapsingToolbarLayout) findViewById(R.id.collapseToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
collapse.setTitle("Title");
collapse.setCollapsedTitleTextColor(Color.parseColor("#FFFFFF"));
collapse.setExpandedTitleColor(Color.parseColor("#FFFFFF"));
collapse.setStatusBarScrimColor(Color.parseColor("#FFFFFF"));
您需要像这样嵌套它:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cor_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="256dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_styled"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="15dp"
android:theme="@style/AppTheme.AppBarOverlay.Styled">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/action_bar_background"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
toolbar= (Toolbar) findViewById(R.id.toolbar);
collapse=(CollapsingToolbarLayout) findViewById(R.id.collapseToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
collapse.setTitle("Title");
collapse.setCollapsedTitleTextColor(Color.parseColor("#FFFFFF"));
collapse.setExpandedTitleColor(Color.parseColor("#FFFFFF"));
collapse.setStatusBarScrimColor(Color.parseColor("#FFFFFF"));
1) 在res文件夹中创建一个tool_bar.xml文件并粘贴以下代码。
2) 如果需要,请包含此tool_bar.xml文件。
3) 在.java文件中实例化tool_bar对象并执行进一步的处理
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/action_bar_background"/>
</android.support.v7.widget.Toolbar>
<include id="@+id/tool_bar"
layout="@layout/tool_bar"/>
Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
工具栏=(工具栏)findViewById(R.id.tool\u栏);
设置支持操作栏(工具栏);
1)在res文件夹中创建一个tool_bar.xml文件,并粘贴以下代码。
2) 如果需要,请包含此tool_bar.xml文件。
3) 在.java文件中实例化tool_bar对象并执行进一步的处理
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/action_bar_background"/>
</android.support.v7.widget.Toolbar>
<include id="@+id/tool_bar"
layout="@layout/tool_bar"/>
Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
工具栏=(工具栏)findViewById(R.id.tool\u栏);
设置支持操作栏(工具栏);
为什么要在AppBarLayout中包装它?您解决了问题?为什么要在AppBarLayout中包装它?您解决了问题?