Android 以编程方式设置ActionBarTab背景色,底部为选项卡选择器线条颜色,用户选择ActionBarTab中的颜色?
我想用底部颜色的选项卡选择器行更改ActionBar的选项卡背景色 我想通过使用java代码而不是xml来实现这一点 我尝试过创建ActionBar选项卡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 = 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,我也初始化了