Android:动作栏主页按钮获得额外的填充

时间:2015-05-08 06:42:51

标签: android android-actionbar android-custom-view

我正在开发一个应用程序,我在其中自定义操作栏,如图所示:

Action Bar

我的代码如下:

// Remove all tabs from actionbar
getActionBar().removeAllTabs();

final ViewGroup actionBarLayout = (ViewGroup)getLayoutInflater().inflate(R.layout.custom_actionbar_layout, null);
// set custom layout for menus on actionbar
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_USE_LOGO);
getActionBar().setCustomView(actionBarLayout);

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setLogo(R.drawable.ic_back_orange);

final TextView textViewOfActionBarHeading = (TextView) actionBarLayout.findViewById(R.id.textView_actionar_heading);
textViewOfActionBarHeading.setText("My Activity");

// Getting the reference of searchViewand TextView of searchView
SearchView searchView = (SearchView) actionBarLayout.findViewById(R.id.searchView_actionar_search);
searchView.setIconifiedByDefault(true);
final TextView search_text = (TextView) searchView.findViewById(searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));

// Set color to TextView of searchView
search_text.setTextColor(Color.BLACK);
// Set hint to TextView of searchView
search_text.setHint("Search");
// Set hintColor to TextView of searchView
search_text.setHintTextColor(Color.DKGRAY);

int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
ImageView v = (ImageView) searchView.findViewById(searchImgId);
v.setImageResource(R.drawable.search_icon);

searchView.setOnSearchClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    }
});

searchView.setOnQueryTextListener(new OnQueryTextListener() {
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText)) {
        } else {
        }
        return true;
    }
});

searchView.setOnCloseListener(new OnCloseListener() {
    @Override
    public boolean onClose() {
    }
});

问题是:我的操作栏后退箭头正在从星标获得额外的填充或边距,如图所示:

enter image description here

1 个答案:

答案 0 :(得分:5)

试试这个

  ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        View customView = getLayoutInflater().inflate(R.layout.main_action_bar, null);
        actionBar.setCustomView(customView);
        Toolbar parent =(Toolbar) customView.getParent();
        parent.setContentInsetsAbsolute(0,0);