编程设置ActionBarTab背景色标签选择线颜色的底部与ActionBarTab用户所选择的颜色? [英] Programmatically set ActionBarTab background color with tab selector line color at bottom with user chosen color in ActionBarTab?

查看:279
本文介绍了编程设置ActionBarTab背景色标签选择线颜色的底部与ActionBarTab用户所选择的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变我的动作条的标签背景色标签选择器线在底部的颜色。

I want to change my ActionBar's Tabs background color with tabs selector line at bottom color.

我想这样做,通过使用Java code不是XML。

I want to do that by using java code not xml.

我曾尝试创建动作条的标签。

I have tried creating ActionBar tabs ..

actionBar = getActionBar();


// Hide the action bar title
           ActionBar actionBar.setDisplayShowTitleEnabled(false);

        // Enabling Spinner dropdown navigation
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

         ActionBar.Tab PlayerTab = actionBar.newTab().setText("Fragment A");
          ActionBar.Tab StationsTab = actionBar.newTab().setText("Fragment B");

          //create the two fragments we want to use for display content
         //////////////////////// Fragment PlayerFragment = new AFragment();
         /////////////////// Fragment StationsFragment = new BFragment();

          //set the Tab listener. Now we can listen for clicks.
          ///////////////////PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
         ///////////////// ////StationsTab.setTabListener(new MyTabsListener(StationsFragment));

          //add the two tabs to the actionbar
          actionBar.addTab(PlayerTab);
          actionBar.addTab(StationsTab);

现在,当我尝试设置背景色标签线路选择的颜色,我得到的错误 Java.lang.NullPointException

Now when I try to set background color with tabs line selector color, I get the error Java.lang.NullPointException

我OnTabSelcted()方法..

My OnTabSelcted() method ..

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView();

//tab.setCustomView(getResources().getDrawable(R.drawable.tabs_selector_blue));

     System.out.println("Tab position is " +tab.getPosition());
     try{
     if(tab.getCustomView() == null){

tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
         tab.setCustomView(tabLayout);
     }else{
         Toast.makeText(getApplicationContext(), "check for tabs", Toast.LENGTH_SHORT).show();
     }
     }catch(Exception e){
         e.printStackTrace();
     }

}

我已经定义了一个自定义选择的背景和需要它充气。

I have defined a custom selector for background and need to inflate it.

我收到就行 tabLayout.setBackgroundResource(TABS_BACKGROUND [tab.getPosition());

请让告诉我哪里是我的错误。

Please let show me where's my error.

推荐答案

我看你使用<一个href="http://stackoverflow.com/questions/18486571/is-it-possible-to-change-actionbar-tab-indicator-programmatically/18491600#18491600">my解决方案。你的 tabLayout 可能为空,如果你创建的标签时没有初始化。这里是code我用第一次创建标签,更改背景和文本颜色 onTabSelected

I see you are using my solution. Your tabLayout may be null if you didn't initialise it when creating tabs. Here is the code I used to create tabs first, before changing background and text color in onTabSelected

for (int i = 0; i < TABS.length; i++) {
    Tab tab = actionBar.newTab();
    tab.setText(TABS[i]);
    tab.setTabListener(this);
    tab.setCustomView(getTabView(i));  << see getTabView() method below  
    actionBar.addTab(tab, i, (currentTab == i));
}

getTabView()方法:

getTabView() method :

private View getTabView(int i) {
    // TODO Auto-generated method stub
    RelativeLayout r = (RelativeLayout) getLayoutInflater().inflate(
            R.layout.custom_tab, null);
    TextView t = (TextView) r.findViewById(R.id.custom_tab_text);
    t.setText(TABS[i]);
    int color;
    switch (i) {
    case MyObject.TYPE_A:
        color = MyObject.COLOR_A;
        break;
    case MyObject.TYPE_B:
        color = MyObject.B;
        break;
    case MyObject.TYPE_C:
        color = MyObject.COLOR_C;
        break;
    case MyObject.TYPE_E:
        color = MyObject.COLOR_E;
        break;
    case MyObject.TYPE_F:
        color = MyObject.COLOR_F;
        break;
    default:
        color = MyObject.COLOR_A;
        break;
    }

    t.setTextColor(color);
    return r;
}

最后, custom_tab.xml ,标签的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp"
    android:layout_margin="0dp">

    <TextView
        android:id="@+id/custom_tab_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:gravity="center|center_horizontal"
        android:textStyle="bold"/>

</RelativeLayout>

这篇关于编程设置ActionBarTab背景色标签选择线颜色的底部与ActionBarTab用户所选择的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆