是否可以仅使用 xml 可绘制对象创建类似 Holo 的 ActionBar 选项卡背景?如果是,怎么办?如果不是,有什么限制?
浏览Android源代码我发现选项卡背景是用tab_indicator_holo.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="@drawable/tab_unselected_holo" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_holo" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />
</selector>
然后为每个状态使用 9 个补丁可绘制对象,例如 tab_selected_holo.9.png .
我想知道这 9 个补丁可绘制对象是否可以替换为图层列表可绘制对象、形状可绘制对象或其组合,从而节省创建各种 PNG 文件的需要(根据我的计算,每个密度 6 个文件)。
我注意到ActionBarSherlock还使用 9 个补丁可绘制对象,因此这很可能是唯一的方法。
最佳答案
要完全自定义您的 ActionBar 选项卡,请针对名为“Home”的虚构选项卡尝试如下操作。此布局包含图像和标签。
(1) 正常创建选项卡,但通过 ActionBar.Tab.setCustomView() 指定自定义布局
// Home tab
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
HomeFragment homeFragment = new HomeFragment();
RelativeLayout layoutView = (RelativeLayout)inflater.inflate(R.layout.layout_tab_home, null);
TextView title = (TextView)layoutView.findViewById(R.id.title);
ImageView img = (ImageView)layoutView.findViewById(R.id.icon);
ActionBar.Tab tabHome = mActionBar.newTab();
tabHome.setTabListener(homeFragment);
title.setText(this.getString(R.string.tabTitleHome));
img.setImageResource(R.drawable.tab_home);
tabHome.setCustomView(layoutView);
mActionBar.addTab(tabHome);
(2) 为您的选项卡创建布局 (layout_tab_home.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="56dip" android:layout_weight="0" android:layout_marginLeft="0dip" android:layout_marginRight="0dip">
<ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:paddingBottom="2dip" />
<TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/icon" android:paddingLeft="0dip" android:paddingRight="0dip" style="@style/tabText" />
</RelativeLayout>
(3) 为图像设置可绘制对象
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/home_sel" />
<item android:state_selected="false" android:drawable="@drawable/home_unsel" />
</selector>
在此示例中,我只有一个 PNG 图形,其中选定状态与未选定状态的颜色不同
您似乎对背景可绘制对象最感兴趣,因此同样的事情也适用,但在您的RelativeLayout上设置背景可绘制对象,并使用与上面类似的选择器。
关于android - ActionBar 选项卡背景与 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11653642/