2

我有一個應用程序,與包含3 ImageView的動作吧,我使用該程序兼容性庫,這裏是代碼:自定義動作條的佈局使用程序兼容性

private void setupActionBar() { 

    // Inflate a "Done/Cancel" custom action bar view. 
     final LayoutInflater inflater = (LayoutInflater) this 
      .getSupportActionBar().getThemedContext() 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View customActionBarView = inflater.inflate(
      R.layout.my_custom_actionbar, null); 
     customActionBarView.findViewById(R.id.title); 



    final ActionBar actionBar = this.getSupportActionBar(); 
     actionBar.setDisplayOptions(
      ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM | 
      ActionBar.DISPLAY_SHOW_HOME | 
      ActionBar.DISPLAY_SHOW_TITLE); 

     actionBar.setCustomView(customActionBarView, 
       new ActionBar.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.MATCH_PARENT 
      ) 
      ); 
} 

的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/gray" > 


<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="fill_horizontal" > 


    <ImageView 
     android:id="@+id/facebook" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left|center_vertical" 
     android:src="@drawable/ic_fb" /> 

    <ImageView 
     android:id="@+id/logo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right|center" 
     android:src="@drawable/ic_hotels" /> 

    <ImageView  
     android:id="@+id/tweetter" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:src="@drawable/ic_tweet" 
     /> 
     </FrameLayout> 
     </RelativeLayout> 

自定義操作欄加載沒有問題,但我不能得到正確的佈局 enter image description here

我試圖做的XML文件b的一些變化它沒有工作。所以我忘了什麼?

+0

什麼是'logo' ImageView的?它會替換左側的應用程序圖標嗎?另外,* FrameLayout *的目的是什麼? – Fllo

回答

3

當我想你的代碼,你貼出來,我有這樣的結果:

________________________________ 
|        | 
| facebook  tweeter  logo | 
|________________________________| 

然後,我猜layout_gravity屬性是錯誤的。此外,你似乎左右顛倒。而FrameLayout似乎沒用在你的情況下( - 我等待你的回答我的評論肯定)。這種佈局可以達到你想要的東西:

<?xml version="1.0" encoding="utf-8"?> 
<!-- Use fill_horizontal to get the whole actionbar 
And center_vertical gravity on all the child views --> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/gray" 
    android:layout_gravity="fill_horizontal" 
    android:gravity="center_vertical" > 

    <!-- Use layout_alignParentRight attribute --> 
    <ImageView 
     android:id="@+id/facebook" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_fb" 
     android:layout_alignParentRight="true" /> 

    <!-- Use layout_alignParentLeft attribute --> 
    <ImageView 
     android:id="@+id/logo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_hotels" 
     android:layout_alignParentLeft="true" /> 

    <!-- Use layout_toLeftOf (id) attribute --> 
    <ImageView  
     android:id="@+id/tweetter" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_tweet" 
     android:layout_toLeftOf="@id/facebook" /> 

</RelativeLayout> 

我得到這樣的輸出:

________________________________ 
|        | 
| logo   tweeter facebook | 
|________________________________| 
+0

它的工作,非常感謝解釋,我剛剛開始android開發..只是一件事,我已經改變了我的標誌的高度和寬度,我想我正在使用一個大圖像 – choco

+0

對於圖標尺寸,你可以請從文檔中查看[Iconography](http://developer.android.com/design/style/iconography.html),特別是[ActionBar的此主題](http://developer.android.com/design/風格/ iconography.html#動作吧)。此外,你有一個[偉大的工具](http://labs.rampinteractive.co.uk/android_dp_px_calculator/)將px轉換爲dp,然後反轉。它可能會幫助你,@choco。 – Fllo