我正在将包含ImageViews的自定义视图添加到选项卡中。
这里我得到布局:
LinearLayout view = (LinearLayout) getLayoutInflater().inflate(
R.layout.custom_actionbar_tab_1, null);
ImageView image1 = (ImageView) view.findViewById(R.id.image1);
image1.setImageResource(R.drawable.icon_28799);
LinearLayout view2 = (LinearLayout) getLayoutInflater().inflate(
R.layout.custom_actionbar_tab_2, null);
ImageView image2 = (ImageView) view2.findViewById(R.id.image2);
image2.setImageResource(R.drawable.icon_20321);
LinearLayout view3 = (LinearLayout) getLayoutInflater().inflate(
R.layout.custom_actionbar_tab_3, null);
ImageView image3 = (ImageView) view3.findViewById(R.id.image3);
image3.setImageResource(R.drawable.icon_39547);
在这里,我将自定义视图设置为选项卡:
actionBar.addTab(actionBar.newTab().setCustomView(view)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setCustomView(view2)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setCustomView(view3)
.setTabListener(this));
但当我在模拟器上测试它时,应用程序的速度会迅速减慢,而且在选项卡之间滑动需要很长时间。图标在一个可绘制的文件夹中。有类似的问题吗?
对于带有图标/文本的选项卡,请尝试下面的代码,确保在TabActivity 中进行扩展
private void setTabs()
{
addTab("Tab1", R.drawable.icon1, Tab1.class);
addTab("Tab2", R.drawable.icon2, Tab2.class);
addTab("Tab3", R.drawable.icon3, Tab3.class);
addTab("Tab4", R.drawable.icon4, Tab4.class);
addTab("Tab5", R.drawable.icon5, Tab5.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#d3d3d3"/>
</LinearLayout>
</TabHost>
然后为选项卡应用您自己的样式。基本上就是这样。如果你在模拟器上仍然遇到崩溃,我建议你尝试genymotion。
问题是由图像大小引起的。我对每一次否认都进行了调整,并解决了这个问题。