Android ActionBar.Tab setCustomView() doesn't fill_parent

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

I'm using ActionBar.Tab setCustomView() method with this layout:

this is my function setting the ActionBar:

public void setActionBar() {     ActionBar actionBar = getSupportActionBar();     //actionBar.hide();     actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);     actionBar.setDisplayShowHomeEnabled(false);     actionBar.setDisplayShowTitleEnabled(false);             //set action bar navigation mode     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);             //set tabs           //home tab      Tab tab = actionBar.newTab().setText(TAB_HOME).setTabListener(new PicoTabListener(this, StartFragment.class));            tab.setCustomView(R.layout.tab_background);     actionBar.addTab(tab);     //events tab     tab = actionBar.newTab().setText(TAB_EVENTS).setTabListener(new PicoTabListener(this, EventsFragment.class));     actionBar.addTab(tab);           //enter event code     tab = actionBar.newTab().setText(TAB_CODE).setTabListener(new PicoTabListener(this, EnterCodeFragment.class));     actionBar.addTab(tab);       } 

and my activity layout:

The results seams to look like this (The upper left tab with the gray background):

How can i make my custom view to fill whole tab and work properly ?

I'm using support package v7 for Android 2.3

回答1:

Just ran into this myself and figured out the solution. You should create a style for the tabview that clears the background and the padding and use it in your theme.

styles.xml:

themes.xml:



回答2:

To every created tab object just add layoutParams:

..     tab.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)); actionBar.addTab(tab); 


回答3:

You have to rely on android:margin and android:padding when dealing with actionbarcompat's setCustomView

keeping in mind that it seems like the root element's padding, width, height, and margin, gets ignored.



回答4:

If you're still having the issue, I found a way to get the whole space : ActionBar Tab with custom View not centered



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!