2013-01-24 39 views
3

我正在創建一個自定義主頁圖標。對於定製的代碼如下所示,如何刪除自定義視圖前的空間?

View customView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null); 
customView.findViewById(R.id.action_home).setOnClickListener(
    new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      toggle(); 
     } 
    }); 

actionBar.setCustomView(customView, new ActionBar.LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
actionBar.setDisplayShowCustomEnabled(true); 

這是action_bar_display_options_custom.xml

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

<ImageView 
    android:id="@+id/action_home" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/btn_home" /> 

<TextView 
    android:id="@+id/action_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> </LinearLayout> 

這是截圖,

這是家中的圖標(即空間不是圖像視圖的一部分),

所以,我怎麼能刪除該視圖的前面的空間?

+2

該空間看起來像是圖像視圖的一部分。將背景顏色設置爲紅色,以查看是否屬於這種情況。如果那是真的,你的圖像會以某種方式縮放。否則,可能只需設置負邊距 – seaplain

+0

@Leo Nguyen您可能在父佈局中設置了填充或給定邊距。你可以在第二張圖像之前看到相同數量的空間,因此我想父母的佈局已經有了填充或邊距 – pvn

+0

我有同樣的問題,當我試圖設置自定義操作欄時,我被卡住了問題 'ActionBar bar = getSupportActionBar(); \t \t bar.setDisplayOptions(0,ActionBar.DISPLAY_SHOW_CUSTOM); \t \t bar.setCustomView(view); \t \t bar.setDisplayShowCustomEnabled(true);' – techieWings

回答

0

刪除您在父佈局中設置的填充或邊距,這將解決問題。請隨時提出疑問,如果能解決您的問題,也請告訴我。

+0

這張圖片正在縮小。謝謝。我解決了 –

+0

這個答案對您有幫助 - 確保標記正確。 – Booger

3

我加入以下行我的代碼

getSupportActionBar().setDisplayShowHomeEnabled(false); 
    getSupportActionBar().setCustomView(view); // set custom view here 
    getSupportActionBar().setDisplayShowTitleEnabled(false); 
    getSupportActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM); 
    getSupportActionBar().setDisplayShowCustomEnabled(true); 

希望這有助於你解決這個問題。

相關問題