0

这是非常简单的,我有这个代码比我从另一个问题获得,并开始分析它,以了解如何使图标位置在ActionBar的左上角:如何在操作栏的左上方放置图标? (或如何消除那空白的空白?)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/linear" 
     android:orientation="vertical" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 
     <TextView 
      android:id="@+id/titleTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="40dp" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="25dp" 
      android:gravity="center_vertical" 
      android:paddingTop="5dp" 
      android:text="Title" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textSize="30dp" /> 
     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" > 
      <TextView 
       android:id="@+id/descriptionTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_marginLeft="25dp" 
       android:height="30dp" 
       android:text="Description" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
     </RelativeLayout> 
    </LinearLayout> 
    <RelativeLayout 
     android:id="@+id/flagLayout" 
     android:layout_width="15dp" 
     android:layout_height="70dp" 
     android:background="#5416B4" 
     android:layout_alignParentLeft="true" /> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="90dp" 
      android:layout_height="70dp" 
      android:layout_alignParentRight="true" 
      android:background="#ffa500" /> 
    </RelativeLayout> 
</RelativeLayout> 

此代码是另一个问题的例子:

Xamarin Android | Layout style

但事实是,由于某种原因,它呈现这样的:

enter image description here

红圈是我加的东西,它是问题的中心,它为什么渲染那个空间?我怎么能消除它?

换句话说,我该如何将一个icone放在actionBar的左上角?

如果你作出答案教导如何改变它的工具栏或类似的东西,并在左上角放置一个图标,它也将被视为一个有效的答案。

感谢您的关注。

回答

0

如果您使用的是AppCompact,工具栏只是一个花哨的Viewgroup,您可以根据需要自定义它。 试试这个:

<android.support.v7.widget.Toolbar 
    style="@style/ToolBarStyle" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" 
    android:minHeight="@dimen/abc_action_bar_default_height_material"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:contentDescription="@string/logo" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:src="@drawable/ic_launcher"/> 
    </android.support.v7.widget.Toolbar> 

这应该带上你的ImageView在工具栏的中心。

在javadoc中,它表示:

一个或多个自定义视图。应用程序可以将任意的子视图添加到工具栏。他们会出现在布局中的这个位置。如果子视图的Toolbar.LayoutParams指示CENTER_HORIZONTAL的重力值,则在所有其他元素都被测量后,视图将尝试居中在工具栏中剩余的可用空间内。

这意味着你不能这样做,除非你创建一个自定义工具栏或删除图标。