noo*_*oob 3 tabs android android-3.0-honeycomb android-actionbar
我希望ActionBar中的所有选项卡都有不同的颜色指示器,例如蓝色表示选项卡1,红色表示选项卡2等.
为了达到这个目的,我确实为所有颜色创建了不同的选择器,并将它们放在不同的xmls中.在style.xml中,我正在调用它们
<style name="MyTheme" parent="AppBaseTheme">
<item name="android:actionBarTabStyle">@style/ActionBarTabStyleRed</item>
</style>
<style name="ActionBarTabStyleRed">
<item name="android:background">@drawable/tab_indicator_red</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我也为不同的颜色创造了这种风格.现在,当我将drawable或样式更改为不同的颜色时,它可以正常工作.我可以看到应用于标签的颜色.但由于所有标签都是相同的颜色,它并没有解决我的目的.我尝试设置标签的样式,onTabSelected()但没有方法可以做到这一点.
最终我尝试为不同的颜色创建不同的主题,并尝试以编程方式设置它们onTabSelected(),但后来我知道必须先设置主题setContentView().
所以我的问题是......我怎么能这样做?有什么办法可以为不同的标签指示器设置不同的颜色???
更新: -
drawable/tab_indicator_red.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_red" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_red" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_red" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_red" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_red" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_red" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_red" />
</selector>
Run Code Online (Sandbox Code Playgroud)
Mat*_*att 13
我想我有一个起点,但它有点繁琐.使用以下代码设置操作栏,您可以设置选项卡以使用自定义外观:
//...somewhere in oncreate
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = bar.newTab();
TextView tv1 = createTab("Tab 1", R.drawable.firsttabdrawable);
tab1.setCustomView(tv1);
tab1.setTabListener(this);
ActionBar.Tab tab2 = bar.newTab();
tab2.setCustomView(createTab("Tab 2", R.drawable.secondTabDrawable));
tab2.setTabListener(this);
ActionBar.Tab tab3 = bar.newTab();
tab3.setCustomView(createTab("Tab 3", R.drawable.thirdTabDrawable));
tab3.setTabListener(this);
ActionBar.Tab tab4 = bar.newTab();
tab4.setCustomView(createTab("Tab 4", R.drawable.fourthTabDrawable));
tab4.setTabListener(this);
bar.addTab(tab1);
bar.addTab(tab2);
bar.addTab(tab3);
bar.addTab(tab4);
Run Code Online (Sandbox Code Playgroud)
createTab方法如下:
private TextView createTab(String titleText, int tabBackgroundDrawableId)
{
TextView tv = new TextView(this);
//set caption and caption appearance
tv.setTextAppearance(this, R.style.MyDefaultTabTextAppearance);
tv.setText(titleText);
//set appearance of tab
tv.setBackgroundResource(tabBackgroundDrawableId);
tv.setLayoutParams(new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
//Make sure all tabs have the same height
tv.setMaxLines(2);
tv.setMinLines(2);
return tv;
}
Run Code Online (Sandbox Code Playgroud)
但是,操作栏似乎将选项卡视图添加到父视图中,该视图添加了填充,因此您需要删除它.我在第一个代码块之后的oncreate中这样做:
View view = (View) tv1.getParent();
LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams)view.getLayoutParams();
view.setPadding(0, 0, 0, 0);
view.setLayoutParams(lp);
Run Code Online (Sandbox Code Playgroud)
你可能会发现一种更优雅/直接的方式,但作为一个起点我觉得值得张贴.
希望这可以帮助.
添加:
值得指出的是,如果您的选项卡具有更复杂的布局,则可以在createTab方法中扩展布局xml文件.例如,使用以下xml(mytab.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tabIcon"/>
<TextView style="@style/MyDefaultTabTextAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tabIcon"
android:minLines="2"
android:maxLines="2"
android:id="@+id/tabText"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
createTab变为:
private RelativeLayout createTab(String titleText, int tabBackgroundDrawableId)
{
RelativeLayout rl = (RelativeLayout)this.getLayoutInflater().inflate(R.layout.mytab, null, false);
//set appearance of tab
rl.setBackgroundResource(tabBackgroundDrawableId);
TextView tv = (TextView)rl.findViewById(R.id.tabText);
//set caption
tv.setText(titleText);
ImageView iv = (ImageView)rl.findViewById(R.id.tabIcon);
iv.setImageResource(R.drawable.ic_launcher);//or supply unique drawable to method?
return rl;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4797 次 |
| 最近记录: |