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>

这是截图,

这是主页的图标(该空间不是图像视图的一部分),

那么,如何删除视图前面的空间?

4

2 回答 2

3

我通过在我的代码中添加以下行来解决这个问题

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

希望这对您有所帮助。

于 2013-01-29T12:56:36.237 回答
0

删除您在父布局中设置的填充或边距,这将解决问题。请随时提出疑问,如果这能解决您的问题,请告诉我。

于 2013-01-24T05:00:16.727 回答