2013-01-24 36 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); 

希望这有助于你解决这个问题。

相关问题