Android 如何在工具栏中居中显示ImageView?
我一直试图在我的工具栏中居中放置一个徽标。当我导航到下一个活动时,“向上启示”图标会出现,它会将我的徽标稍微向右推。如何在不删除“向上启示”图标的情况下将徽标保持在工具栏的中心位置 这是我的工具栏标签Android 如何在工具栏中居中显示ImageView?,android,android-actionbar,imageview,center,toolbar,Android,Android Actionbar,Imageview,Center,Toolbar,我一直试图在我的工具栏中居中放置一个徽标。当我导航到下一个活动时,“向上启示”图标会出现,它会将我的徽标稍微向右推。如何在不删除“向上启示”图标的情况下将徽标保持在工具栏的中心位置 这是我的工具栏标签 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
<android.support.v7.widget.Toolbar
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="wrap_content"
android:background="@color/primaryColor"
android:paddingTop="@dimen/app_bar_top_padding"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
app:theme="@style/CustomToolBarTheme">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_android_logo"
android:layout_gravity="center"
android:paddingBottom="3dp"/>
</android.support.v7.widget.Toolbar>
在中,它表示:
一个或多个自定义视图。应用程序可以添加任意子对象
将视图添加到工具栏。它们将出现在
布局。如果子视图的Toolbar.LayoutParams指示重力
水平视图的中心值将尝试在
完成所有其他元素后工具栏中剩余的可用空间
已测量
这意味着除非创建自定义的
工具栏
或删除图标
,将图像设置为工具栏的背景,而不是子视图,否则无法执行此操作
<android.support.v7.widget.Toolbar
android:id="@+id/detail_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="@drawable/my_image"
app:theme="@style/CustomToolBarTheme">
您可以按以下步骤进行操作,这对我很有用:
<androidx.appcompat.widget.Toolbar
android:id="@+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="@dimen/abc_action_bar_default_height_material">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/headerLogo"
android:contentDescription="@string/app_name" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
您可以通过编程实现,这是我唯一能够在工具栏中将徽标完全居中的方法 添加到您的活动中:
@Override
public void onWindowFocusChanged(boolean hasFocus){
// set toolbar logo to center programmatically
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
ImageView logo = (ImageView) findViewById(R.id.logo);
int offset = (toolbar.getWidth() / 2) - (logo.getWidth() / 2);
// set
logo.setX(offset);
}
以及您的工具栏_main.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/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/bg_yellow"
android:elevation="4dp"
android:fontFamily="@string/font_family_thin"
app:contentInsetStartWithNavigation="0dp"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/title_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@string/font_family_light"
android:textColor="@color/text_dark_xxx"
android:textSize="@dimen/text_default"
android:layout_centerVertical="true"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/logo"
android:id="@+id/logo"
android:paddingTop="5dp" />
</android.support.v7.widget.Toolbar>
我回答这个问题有点晚了,但有很好的解决方案
<android.support.design.widget.AppBarLayout 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="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:contentDescription="@string/app_name"
android:id="@+id/logoImageView"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:src="@drawable/toolbar_background" />
</RelativeLayout>
</FrameLayout>
</android.support.design.widget.AppBarLayout>
适用于使用矢量可绘制或SVG图像的开发人员。
这可以是工具栏的背景,可绘制的工具栏背景.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/primary"/>
</shape>
</item>
<item
android:width="@dimen/toolbar_app_icon_width"
android:height="@dimen/toolbar_app_icon_height"
android:drawable="@drawable/ic_app_logo"
android:gravity="center"/>
</layer-list>
是的,您必须在drawable内部固定项目的宽度和高度,尽管vector drawable已经具有固定的大小。
这是您的工具栏视图
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/drawable_toolbar_background"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"/>
您需要将AppBarLayout小部件与工具栏小部件一起使用,并将ContentInsertStart
设置为0dp
。否则,工具栏将在开始时显示大约8dp的填充(如果需要,插入导航按钮)
以下是工作代码:
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:fitsSystemWindows="false"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetStart="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@drawable/company_logo" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
此外,请确保使用?attr/actionBarSize
作为工具栏的高度,因为这是默认的操作栏高度,并将根据所有不同的屏幕大小进行调整。即使存在菜单操作,也可以使用的最简单的解决方案是:
<android.support.design.widget.AppBarLayout
android:id="@+id/apbMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/tlbMain"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
app:navigationContentDescription="@null"
app:title="@null"
app:subtitle="@null"
app:popupTheme="@style/AppTheme.PopupOverlay">
<ImageButton
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:scaleType="centerInside"
android:background="@null"
android:onClick="onBackClickListener"
android:src="@drawable/ic_navigation_back"
android:visibility="@{showBackBtn ? View.VISIBLE : View.INVISIBLE}" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:src="@drawable/toolbar_icon" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
我想出了这个解决方案。将工具栏
和图像视图
放在ConstraintLayout
内,并将其所有侧面约束到父对象:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="@drawable/icon" />
<ImageView
android:layout_width="148dp"
android:layout_height="@dimen/dim_24x"
android:contentDescription="@string/accs_label"
android:src="@drawable/label"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我觉得这是最简单的方法。当然,在小型手机上,如果有很多菜单项,图像视图将与菜单项重叠
我发现很难将视图放在工具栏布局本身的中心位置,因为位置是相对于导航按钮和菜单项计算的。因此,它将在导航按钮和菜单项之间居中,而不是在实际的工具栏开始和结束之间。将属性android:contentInsetStart和0dp放在工具栏中
使用LayerList drawable,您还可以通过将徽标设置为位图并在其下方(xml顶部)放置一个彩色实体来设置背景色。这是因为它没有设置在工具栏主题上。我敢肯定,如果你在工具栏上添加菜单项,这将不起作用,因为这样会缩短相对窗口的宽度。不知怎的,你的解决方案对我不起作用。图标将被拉伸以适应工具栏的宽度。你有没有经历过这种行为?这是迄今为止我发现的向量可拖动设备的关闭…@DominikusK。是的,我确实有这个问题。这就是为什么您必须在可绘制的工具栏背景中固定项目的宽度和高度。我将强调它。我认为这应该是公认的答案,除了这个,什么都不管用!由于菜单项大小不同而失败。当应用程序栏只有菜单图标时,应接受此答案。谢谢你的解决方案。从上面的例子中,你可以看到目前只有一个TextView和一个ImageView,所以如果你正在膨胀菜单,你可能还需要测量图标!提示,也许您可以进入“工具”>“布局检查器”查看实际情况。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="@drawable/icon" />
<ImageView
android:layout_width="148dp"
android:layout_height="@dimen/dim_24x"
android:contentDescription="@string/accs_label"
android:src="@drawable/label"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>