2015-10-14 46 views
2

我在Xamarin.Android中创建了一个布局,我正在使用图像按钮TextView和另一个标签来自定义工具栏。我需要做的是确保图像按钮BackButton一直左对齐到父容器的左侧,TextView是中心对齐,并且MenuButton图像按钮右对齐一直到父容器的右侧。下面你会看到我在设计模式的看法:如何在Android xml中自定义工具栏上对齐UI控件

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"> 

enter image description here

回答

1

将一个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在另一个按钮上保证他们将坚持RelativeL的左侧和右侧包含它们的ayout。它之前没有工作,因为工具栏不是RelativeLayout。

要删除默认的利润率为工具栏,请在这个问题上的说明:Android API 21 Toolbar Padding

性质contentInsetStart和contentInsetLeft必须为零。

+0

您是否指的是我所做的包括xml abover?否则,ImageButton的父对象是一个工具栏 – yams

+0

对不起,我误解了你的问题,我现在编辑我的回答 –

+0

这没有奏效。这使得相对布局比宽度上的工具栏略小,并使MenuButton ImageButton消失在屏幕之外。 – yams

0

我知道这已被回答,但不能只使用layout_gravity而不是relativelayout?

喜欢:

android:layout_gravity="right" 
    android:layout_gravity="center" 
    android:layout_gravity="left" 

我写这篇文章,因为如果你要添加的东西在后面的代码的工具栏,它会推相对布局在一旁,其所有内容....我想所以至少!:)

+0

不,因为所有的控制都在axml中(在听众或委托人之外)。 – yams