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的重力值,則在所有其他元素都被測量後,視圖將嘗試居中在工具欄中剩餘的可用空間內。

這意味着你不能這樣做,除非你創建一個自定義工具欄或刪除圖標。