如何在Android xml中对齐自定义工具栏上的UI控件
我正在Xamarin.Android中创建一个布局,我正在使用一个带有图像按钮、文本视图和另一个标签的自定义工具栏。我需要做的是确保imagebutton BackButton与父容器的左侧对齐,TextView与父容器的右侧对齐,MenuButton Image与父容器的右侧对齐。下面您将看到我在设计模式下的视图: axml代码:如何在Android xml中对齐自定义工具栏上的UI控件,android,xamarin.android,toolbar,Android,Xamarin.android,Toolbar,我正在Xamarin.Android中创建一个布局,我正在使用一个带有图像按钮、文本视图和另一个标签的自定义工具栏。我需要做的是确保imagebutton BackButton与父容器的左侧对齐,TextView与父容器的右侧对齐,MenuButton Image与父容器的右侧对齐。下面您将看到我在设计模式下的视图: axml代码: <?xml version="1.0" encoding="utf-8"?> <Toolbar xmlns:android="http://sch
<?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 xmlns:android=”http://schemas.android.com/apk/res/android"
android:layout\u width=“fill\u parent”
android:layout\u height=“包装内容”
android:layout\u centerHorizontal=“true”
android:gravity=“center_vertical | center_horizontal”>
在工具栏中放置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的左侧和右侧。它以前不工作,因为工具栏不是相对的工具
要删除工具栏的默认边距,请按照此问题的说明进行操作:
属性contentInsetStart和contentInsetLeft必须为零。我知道这已经得到了回答,但是你不能用layout而不是relativelayout吗 比如:
android:layout_gravity="right"
android:layout_gravity="center"
android:layout_gravity="left"
我写这篇文章是因为,如果您想在以后的代码中向工具栏添加内容,它会将相关布局及其所有内容推到一边。。。。我想至少是这样!:) 您指的是上面我所说的包含xml的地方吗?否则ImageButton的父对象是工具栏对不起,我误读了你的问题,我将立即编辑不起作用的答案。这使得相对布局略小于工具栏的宽度,并使菜单按钮ImageButton在屏幕上消失。对不起,我错过了属性的名称。它是android:layout\u alignParentRight=“true”和android:layout\u alignParentLeft=“true”基本上仍然存在问题。如果转到设计视图,它会显示在按钮边缘结束的相对布局。我将发布一个pic.No,因为所有控件都在axml中(监听器或委托之外)。