Nic*_* Oz 65 android android-toolbar
使用最后一个android.support.v7.widget.Toolbar
我想在工具栏中居中一个图像,但它一直停留在左边.
对我来说最好的方法是使用toolbar.setLogo()
方法并使徽标居中,但我看不到任何方法.
我使用的布局Toolbar
:
<android.support.v7.widget.Toolbar style="@style/ToolBarStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="@dimen/abc_action_bar_default_height_material">
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)
是否有可能在工具栏中添加图像(徽标)并使其居中?以编程方式或在布局中?
Abh*_*uri 126
工具栏只是一个ViewGroup,您可以根据需要自定义.
试试这个 :
<android.support.v7.widget.Toolbar
style="@style/ToolBarStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="@dimen/abc_action_bar_default_height_material">
<ImageView
android:layout_width="wrap_content"
android:contentDescription="@string/logo"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_launcher"/>
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)
这应该将您的imageView带到工具栏的中心.
Blu*_*rry 33
如果您有任何视图并且无法使徽标居中,那将是因为居中是基于剩余空间而不是整个工具栏.
要确保徽标居中,请尝试使用图层列表作为工具栏的背景,而不是尝试将图像用作视图.
toolbar.xml
<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"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/toolbar_background"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="pin">
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)
toolbar_background.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimary" />
</shape>
</item>
<item>
<bitmap android:src="@drawable/logo" android:gravity="center" />
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
这应该确保具有特定背景颜色的居中徽标.
这不是最好的解决方案,但即使你有按钮或菜单项,它也可以解决.
尝试
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
style="@style/ToolBarStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="@dimen/abc_action_bar_default_height_material"/>
<ImageView
android:layout_width="wrap_content"
android:contentDescription="@string/logo"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_launcher"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
我喜欢Ruben Yoo 的回答给出的方法,因为它很简单。它是明确的,易于遵循,并将所有代码更改放在一起。
这是一个更新的版本,有一些小的改进:
<android.support.design.widget.AppBarLayout
android:id="@+id/layout_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name"
android:scaleType="centerInside"
android:src="@mipmap/ic_launcher"/>
</FrameLayout>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
变化:
AppBarLayout
父母?attr/actionBarSize
提供给框架和工具栏的显式大小ImageView
在宽度和高度上匹配父尺寸centerInside
归档时间: |
|
查看次数: |
47555 次 |
最近记录: |