Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式设置ActionBarTab背景色,底部为选项卡选择器线条颜色,用户选择ActionBarTab中的颜色?_Android_Tabs_Android Actionbar - Fatal编程技术网

Android 以编程方式设置ActionBarTab背景色,底部为选项卡选择器线条颜色,用户选择ActionBarTab中的颜色?

Android 以编程方式设置ActionBarTab背景色,底部为选项卡选择器线条颜色,用户选择ActionBarTab中的颜色?,android,tabs,android-actionbar,Android,Tabs,Android Actionbar,我想用底部颜色的选项卡选择器行更改ActionBar的选项卡背景色 我想通过使用java代码而不是xml来实现这一点 我尝试过创建ActionBar选项卡 actionBar = getActionBar(); // Hide the action bar title ActionBar actionBar.setDisplayShowTitleEnabled(false); // Enabling Spinner dropdown navigatio

我想用底部颜色的选项卡选择器行更改ActionBar的选项卡背景色

我想通过使用java代码而不是xml来实现这一点

我尝试过创建ActionBar选项卡

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);
现在,当我尝试使用tabs line selector color设置背景色时,会出现错误
Java.lang.NullPointException

我的ontAbcelected()方法

@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();
     }

}
我已经为背景定义了一个自定义选择器,需要对其进行充气

我正在上网
tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()])

请告诉我我的错误在哪里。

请参阅声明

RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView();
if
条件

if(tab.getCustomView() == null){
     tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
     tab.setCustomView(tabLayout);
}
现在,如果
tab.getCustomView()
返回
null
,那么
tabLayout
也是
null
,并且
if
条件变为
true
。在
条件中,如果
条件,则引用的是
tabLayout
,即
null

请参见该语句

RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView();
if
条件

if(tab.getCustomView() == null){
     tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
     tab.setCustomView(tabLayout);
}
现在,如果
tab.getCustomView()
返回
null
,那么
tabLayout
也是
null
,并且
if
条件变为
true
。在
条件中,如果
条件,则引用的是
tabLayout
,即
null

请参见该语句

RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView();
if
条件

if(tab.getCustomView() == null){
     tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
     tab.setCustomView(tabLayout);
}
现在,如果
tab.getCustomView()
返回
null
,那么
tabLayout
也是
null
,并且
if
条件变为
true
。在
条件中,如果
条件,则引用的是
tabLayout
,即
null

请参见该语句

RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView();
if
条件

if(tab.getCustomView() == null){
     tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
     tab.setCustomView(tabLayout);
}
现在,如果
tab.getCustomView()
返回
null
,那么
tabLayout
也是
null
,并且
if
条件变为
true
。在
条件中,如果
条件,您正在引用
表格布局
,它是
空的

我看到您正在使用。如果在创建选项卡时未初始化,则您的
表格布局可能为空。下面是我用来先创建选项卡的代码,然后在
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));
}
最后,
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>

我看到您正在使用。如果在创建选项卡时未初始化,则您的
表格布局可能为空。下面是我用来先创建选项卡的代码,然后在
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));
}
最后,
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>

我看到您正在使用。如果在创建选项卡时未初始化,则您的
表格布局可能为空。下面是我用来先创建选项卡的代码,然后在
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));
}
最后,
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>

我看到您正在使用。如果在创建选项卡时未初始化,则您的
表格布局可能为空。下面是我用来先创建选项卡的代码,然后在
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));
}
最后,
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>



您是否初始化了
选项卡\u BACKGROUND
数组?是的,我初始化了。。私有静态final int[]TABS_BACKGROUND={R.drawable.TABS_selector_blue,R.drawable.TABS_selector_blue,R.drawable.TABS_selector_blue};因此,
tabLayout
为空,请参见Gopal回答是的,我的tabLayout为空。您是否初始化了
TABS\u BACKGROUND
数组?是的,我初始化了。。私有静态final int[]TABS_BACKGROUND={R.drawable.TABS_selector_blue,R.drawable.TABS_selector_blue,R.drawable.TABS_selector_blue};因此,
tabLayout
为空,请参见Gopal回答是的,我的tabLayout为空。您是否初始化了
TABS\u BACKGROUND
数组?是的,我初始化了。。私有静态final int[]TABS_BACKGROUND={R.drawable.TABS_selector_blue,R.drawable.TABS_selector_blue,R.drawable.TABS_selector_blue};因此,
tabLayout
为空,请参见Gopal回答是的,我的tabLayout为空。您是否初始化了
TABS\u BACKGROUND
数组?是的,我初始化了。。私有静态final int[]TABS_BACKGROUND={R.drawable.TABS_selector_blue,R.drawable.TABS_selector_blue,R.drawable.TABS_selector_blue};因此,
tabLayout
为空,请参见Gopal回答是,我的tabLayout为空,我已初始化为。。RelativeLayout表格布局=(RelativeLayout)选项卡。getCustomView();。是否需要执行其他操作?已
选项卡。getCustomView()
正在返回
null
。那你怎么能说它已经初始化了呢?将
tabLayout
分配给您想要的
视图…@AndroidHacker,检查我的答案。你没有像Gopal Rao说的那样初始化选项卡自定义视图。这就是为什么你要得到NPE。我已经初始化为。。RelativeLayout表格布局=(RelativeLayout)选项卡。getCustomView();。是否需要执行其他操作?已
选项卡。getCustomView()
正在返回
null
。那你怎么能说它已经初始化了呢?将
tabLayout
分配给您想要的
视图…@AndroidHacker,检查我的答案。你没有像Gopal Rao说的那样初始化选项卡自定义视图。这就是为什么你要得到NPE。我已经初始化为。。RelativeLayout表格布局=(RelativeLayout)选项卡。getCustomView();。是否需要执行其他操作?已
选项卡。getCustomView()
正在返回
null
。那你怎么能说它已经初始化了呢?将
tabLayout
分配给您想要的
视图…@AndroidHacker,检查我的答案。你没有像Gopal Rao说的那样初始化选项卡自定义视图。这就是为什么你会得到NPE,我也初始化了