Xml 在自定义操作栏中制作居中徽标
我有一个使用android studio的移动项目。 我想知道如何使我的标志中心在行动吧。 在我将一个项目放入操作栏后,徽标没有居中 这是我的自定义操作栏视图Xml 在自定义操作栏中制作居中徽标,xml,android-layout,kotlin,Xml,Android Layout,Kotlin,我有一个使用android studio的移动项目。 我想知道如何使我的标志中心在行动吧。 在我将一个项目放入操作栏后,徽标没有居中 这是我的自定义操作栏视图 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_actionbar_relative_layout_main"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="vertical"
android:background="@color/transparent"
android:enabled="false">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:padding="10dp"
android:src="@drawable/ic_logo"
android:contentDescription="@string/logo" />
</LinearLayout>
lol nice-好吧,它是居中的,只是当你添加一个菜单时,图标会占用一点空间(如果你允许其他图标进入工具栏,其他图标也会占用一点空间),所以图像会居中于剩余空间。我现在还不知道,但我假设菜单图标是48dp,所以试着在另一边加上那个数量的边距(你现在有5dp)。你可以使用Layout Inspector在运行的模拟器上检查它,或者我尝试过使用边距添加空间的任何东西,但是当我在不同的设备上使用它时,边距是不同的。这里有一些不同的方法:主要是尝试将重力值设置为
中心
,有人举了一个在代码中设置它的例子(通过计算屏幕的宽度并从中设置位置)
supportActionBar?.apply {
displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
setDisplayShowCustomEnabled(true)
setCustomView(R.layout.custom_action_bar)
elevation = 3f
}