2013-10-12 112 views
2

我都忍了在Android操作欄微調,我已經放置在佈局的test.xml微調,我在OnCreate()方法調用它像這樣的主要活動標題上沒有顯示在MainActivity

ActionBar actionBar = getActionBar(); 
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.GREEN)); 
    actionBar.setDisplayShowCustomEnabled(true); 

    actionBar.setCustomView(R.layout.activity_test); 
    setContentView(R.layout.activity_main); 
    actionBar.setTitle("Home"); 

但我的標題沒有顯示。我想在該操作欄上顯示HOME作爲標題,我應該怎麼做?

我activity_test

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

    <Spinner 
     android:id="@+id/planets_spinner" 
     android:layout_width="100dp" 
     android:layout_height="50sp" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/arrow2" 
     android:layout_weight="0.08" 
     android:drawSelectorOnTop="true" /> 

</RelativeLayout> 
+0

你可以發佈activity_text.xml –

+0

代碼已添加 –

回答

1

我已經通過添加一個TextView我activity_test解決了這個問題,因爲這整個活動表示這種動作條,所以我增加了一個文本視圖和問題解決:)

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


    <Spinner 
     android:id="@+id/planets_spinner" 
     android:layout_width="100dp" 
     android:layout_height="50sp" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/arrow2" 
     android:drawSelectorOnTop="true" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/planets_spinner" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="6dp" 
     android:layout_marginLeft="6dp" 
     android:textStyle="bold" 
     android:textSize="20sp" 
     android:textColor="#ffffff" 
     android:text="Scene" /> 

</RelativeLayout> 
0

當您設置android:layout_alignParentRight="true"時,RelativeLayout將使用包括標題空間和標題在內的所有空白不顯示。您可以通過在RelativeLayout周圍繪製邊框來檢查行爲。但是如果你不使用android:layout_alignParentRight="true",RelativeLayout不會消耗更多的空間。如果您也使用android:layout_alignParentLeft="true",它不會消耗更多空間。它奇怪的只有android:layout_alignParentRight="true"。爲了顯示父母的權利,以下工作適用於我:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/profile_image" 
     android:layout_width="40dp" 
     android:src="@drawable/ic_launcher" 
     android:layout_height="40dp" 
     android:background="?android:selectableItemBackground" 
     android:padding="3dp" 
     android:scaleType="centerCrop" /> 

請勿使用RelativeLayout。只是你需要的觀點。 然後你可以右對齊與顯示爲繼的LayoutParams:

actionBar.setCustomView(R.layout.actionbar_layout); 
actionBar.setDisplayShowCustomEnabled(true); 
ImageView imageProfile = (ImageView)actionBar.getCustomView(); 
int length = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics()); 
Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(
       length, 
       length, Gravity.RIGHT 
       | Gravity.CENTER_VERTICAL); 
imageProfile.setLayoutParams(layoutParams); 

現在你應該可以看到這兩個動作條標題和ImageView的。