【问题标题】:Styling actionbar tabs - the easy way样式化操作栏标签 - 简单的方法
【发布时间】:2014-07-11 17:50:07
【问题描述】:

我正在尝试将自定义样式应用于操作栏选项卡,但目前没有任何成功。我知道那里有很多教程,但有人可以解释一下应该如何完成,或者提供指向优秀教程或文档的链接,了解如何实现自定义样式。

这是我的代码:

ActionBar actionBar = getActionBar();
Tab1 = actionBar.newTab().setText("TAB1");
        Tab2 = actionBar.newTab().setText("Tab2");
        // Set Tab Listeners
        Tab1.setTabListener(new TabListener(fragmentTab1));
        Tab2.setTabListener(new TabListener(fragmentTab2));
        // Add tabs to actionbar
        actionBar.addTab(Tab1);
        actionBar.addTab(Tab2);

我想要达到的目标:

【问题讨论】:

    标签: android android-actionbar android-tabs android-styles android-style-tabhost


    【解决方案1】:

    这是我为我的一个项目编写的一段代码,您应该可以根据自己的需要轻松调整它!

    //@param string - the R.string.name_of_string, I use it for convenience
    private ActionBar.Tab createCustomTab(ActionBar actionBar, int string){
        View tabView = getLayoutInflater().inflate(R.layout.tab, null);
    
        ((TextView) tabView.findViewById(R.id.txt_tab)).setText(string);
    
        //in my case I wanted to center the layout - for some reason certain layout params do not work in the XML,
        //  (such as centering), so I do that here
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER;
        tabView.setLayoutParams(lp);
    
        return actionBar.newTab().setCustomView(tabView);
    }
    

    如果您有任何问题,请告诉我!

    【讨论】:

    • 您的代码似乎使选项卡膨胀,但似乎原始选项卡具有某种填充 - 并且原始选项卡设计在背景中可见。看这里:postimg.org/image/94fvypsi7
    • 对于我的解决方案,我使用它来更改文本样式并在选项卡中添加可更改的 ImageView...如果您尝试更改选项卡的颜色,您是否尝试过 this ?
    • 另外,如果填充导致问题,也许您可​​以使用选项卡父级上的布局参数...(查看)tab.getParent()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 2014-04-16
    相关资源
    最近更新 更多