2014-01-07 78 views
11

我剛從IOS開始進行Android開發,並再次茬問題。 經過1天的嘗試,我決定我會問堆棧溢出的人。Android ActionBar自定義佈局樣式

所以我的問題:

我有一個應用程序,並在操作欄(默認沒有福爾摩斯),我想1個標誌和2行文字。 並通過互聯網我發起了setCustomView的行動欄。 而自定義的XML被加載,但我無法得到正確的佈局。

所以首先我安裝操作欄:

private void setupActionBar() { 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayShowTitleEnabled(false); 
    actionBar.setDisplayUseLogoEnabled(false); 
    actionBar.setDisplayHomeAsUpEnabled(false); 
    actionBar.setDisplayShowCustomEnabled(true); 
    actionBar.setDisplayShowHomeEnabled(false); 

    LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    View customNav = LayoutInflater.from(this).inflate(R.layout.actionbar, null); // layout which contains your button. 

    actionBar.setCustomView(customNav, lp1); 
} 

比XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="fill_horizontal" 
    android:orientation="horizontal" 
    android:background="@color/Blue"> 

    <TextView 
     android:id="@+id/text_left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/title_menu" 
     android:textColor="@color/White" 
     android:paddingLeft="5dp" 
     android:layout_gravity="left"/> 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/Icon" 
     android:layout_gravity="center"/> 

    <TextView 
     android:id="@+id/text_right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/title_help" 
     android:layout_gravity="right" 
     android:textColor="@color/White" 
     android:paddingRight="5dp"/> 

</LinearLayout> 

但結果是什麼事我不是在尋找:

Problem in an image

那麼我忘了/做錯了什麼/或者我該怎麼做?

回答

13

嘗試更改根佈局成相對佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_gravity="fill_horizontal" 
android:orientation="horizontal"> 

<TextView 
    android:text="left text" 
    android:layout_toLeftOf="@+id/icon" 
    android:layout_alignParentLeft="true" 
    android:id="@+id/text_left" 
    android:gravity="left" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="5dp"/> 

<ImageView 
    android:layout_centerHorizontal="true" 
    android:id="@+id/icon" 
    android:layout_width="10dp" 
    android:layout_height="wrap_content" 
    android:src="@drawable/bg_sunrise" 
    android:layout_gravity="center"/> 

<TextView 
    android:text="Right txt" 
    android:layout_toRightOf="@+id/icon" 
    android:id="@+id/text_right" 
    android:layout_width="match_parent" 
    android:gravity="right" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:paddingRight="5dp"/> </RelativeLayout> 
+0

我愛你,它的完成我細腰1天。現在我可以繼續。謝謝 – Msmit1993

+0

不客氣^^ –

+1

謝謝這很有用。如果您需要不同的佈局,請始終以root身份使用RelativeLayout! – Gibberish

2

你沒有得到你的結果是因爲:

enter image description here

ü需要:

  1. 如果使用程序兼容性主題

enter image description here

  • 添加到活動佈局
  • enter image description here

    在代碼例如
  • 。在onCreate()中設置一個工具欄來替換操作欄。
  • enter image description here