我正在 Xamarin.Android 中创建一个布局,我正在处理一个带有图像按钮、TextView 和另一个标签的自定义工具栏。我需要做的是确保图像按钮 BackButton 一直左对齐到父容器的左侧,TextView 居中对齐,MenuButton 图像按钮一直右对齐到父容器的右侧。下面您将看到我在设计模式下的 View :
axml代码:
<?xml version="1.0" encoding="utf-8"?>
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ToolBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar"
android:popupTheme="@android:style/ThemeOverlay.Material.Light">
<ImageButton
android:id="@+id/BackButton"
android:layout_width="wrap_content"
android:background="@drawable/RedButton"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:src="@drawable/back48x48"
style="@style/back_button_text" />
<TextView
android:id="@+id/AppNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:layout_marginLeft="18dp"
android:layout_marginRight="12dp"
android:textSize="25dp"
android:text="Just a Label ok" />
<ImageButton
android:id="@+id/MenuButton"
android:layout_width="wrap_content"
android:layout_marginLeft="2dp"
android:layout_centerVertical="true"
android:background="@drawable/RedButton"
android:layout_alignParentRight="true"
android:gravity="right"
android:layout_height="wrap_content"
android:src="@drawable/menu48x48"
style="@style/menu_button_text" />
</Toolbar>
只是想知道最好的方法。
这是在下面包含的另一个 axml 中调用的方式:
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center_vertical|center_horizontal">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="3">
最佳答案
在您的工具栏中放置一个 RelativeLayout 并执行以下操作:
<?xml version="1.0" encoding="utf-8"?>
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ToolBar"
android:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar"
android:popupTheme="@android:style/ThemeOverlay.Material.Light">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/BackButton"
android:layout_width="wrap_content"
android:background="@drawable/RedButton"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:src="@drawable/back48x48"
android:layout_alignParentLeft="true"
style="@style/back_button_text" />
<TextView
android:id="@+id/AppNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:layout_marginLeft="18dp"
android:layout_marginRight="12dp"
android:textSize="25dp"
android:text="Just a Label ok" />
<ImageButton
android:id="@+id/MenuButton"
android:layout_width="wrap_content"
android:layout_marginLeft="2dp"
android:layout_centerVertical="true"
android:background="@drawable/RedButton"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:src="@drawable/menu48x48"
style="@style/menu_button_text" />
</RelativeLayout>
</Toolbar>
后退按钮上的 alignParentLeft 属性和另一个按钮上的 alignParentRight 属性保证它们将粘在包含它们的 RelativeLayout 的左侧和右侧。之前它不起作用,因为工具栏不是 RelativeLayout。
要删除工具栏的默认边距,请按照以下问题的说明进行操作:Android API 21 Toolbar Padding
属性 contentInsetStart 和 contentInsetLeft 必须为零。
关于android - 如何在 Android xml 中的自定义工具栏上对齐 UI 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33134332/