我正在尝试将徽标设置为Android工具栏中的背景。我试图将图像直接放在工具栏中,但这会导致徽标没有被集中。这是我现在有的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="@drawable/Toolbar_background"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
和可绘制的工具栏_beackground:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<item>
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
</shape>
</item>
<item>
<bitmap
android:contentDescription="Unleashed logo"
android:src="@drawable/unleashed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</item>
</layer-list>
这有效,但徽标宽度伸展,没有填充的填充,位图占用了它可以得到的所有空间。
https://www.imgdumper.nl/uploads9/5a796ede6c77c/5a796ede6bd4d-capture.png
您可以创建自定义工具栏。
检查下面的代码。
toolbar_bg_image.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/colorAccent"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:layout_collapseMode="pin"
app:popupTheme="@style/toolbarTheme">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/_4sdp"
android:paddingRight="@dimen/_4sdp"
android:paddingStart="@dimen/_4sdp"
android:paddingEnd="@dimen/_4sdp"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_marginRight="@dimen/_20sdp"
android:scaleType="fitXY"
app:srcCompat="@drawable/unlashed"/>
</android.support.v7.widget.Toolbar>
</layout>
我建议您在 android中的要求中拿出图像,并设置背景颜色:background ="@color/coloraccent" <</i>
您还可以根据需要在ImageView中设置边距和填充。
现在,您可以在下面的布局文件中包含此工具栏。
fragment_or_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar_bg_image" />
</LinearLayout>
</layout>
您可以尝试以下设置徽标:
<Toolbar
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:id="@+id/atc_toolbar"
android:background="?android:attr/colorPrimaryDark"
android:elevation="4dp"
android:navigationIcon="@android:drawable/ic_menu_compass"
android:logo="@android:drawable/ic_menu_view"
android:title="My application">
</Toolbar>