我正在创建一个Android应用程序,我的xml中有一个tabhost和两个标签。我打电话给他们,他们工作得很好。但我想摆脱标签上丑陋的灰色/橙色。我尝试使用图像设置bg。我用了这段代码:
th.getTabWidget().setBackgroundResource(R.drawable.tab_normal);
但它显示如下:
如何才能让它取代灰色和橙色?
由于
答案 0 :(得分:1)
完全自定义tabwidget的步骤:
为tabwidget创建自定义布局:此布局类似于默认布局,由一个Icon
和一个TextView
组成。您可以选择所需的任何布局。请注意,父布局具有有状态背景tabwidget_selector
。这个drawable是替换默认橙色焦点颜色的关键。您可以选择自己的焦点颜色。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/tabwidget_selector"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/tabIndicatorIcon"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginTop="2dp" />
<TextView
android:id="@+id/tabIndicatorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:textColor="@color/white" />
写入返回tabwidget
视图的函数。为tabwidget设置文本,图标drawable
private View getTabIndicatorView(Context context, String tag, int drawable) {
View view = LayoutInflater.from(context).inflate(
R.layout.tab_widget_custom, null);
TextView tv = (TextView) view.findViewById(R.id.tabIndicatorText);
tv.setText(tag);
ImageView tabIndicatorIcon = (ImageView) view
.findViewById(R.id.tabIndicatorIcon);
tabIndicatorIcon.setBackgroundResource(drawable);
return view;
}
在您的活动中使用此功能:
TabHost.TabSpec setIndicator(视图视图)将视图指定为选项卡 指示器。
Intent intent;
TabHost.TabSpec spec;
spec = tabHost.newTabSpec("Inbox").setIndicator(
getTabIndicatorView(MainTabActivity.this, "Hộp thư",
R.drawable.tin_nhan_tab_selector));
intent = new Intent(this, xxxx.InboxActivity.class);
spec.setContent(intent);
tabHost.addTab(spec);
答案 1 :(得分:0)
您可以获得tabwidget的背景信息:
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/tabbackground"
android:layout_margin="7dp"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@android:id/tabs"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:background="@drawable/list_shape">
</FrameLayout>
</RelativeLayout>