我正在制作一个 android 应用程序,我的 xml 中有一个 tabhost 和两个选项卡。我给他们打过电话,他们工作得很好。但我想摆脱标签上丑陋的灰色/橙色。我尝试使用图像设置背景。我使用了这段代码:
th.getTabWidget().setBackgroundResource(R.drawable.tab_normal);
但它是这样显示的:
我怎样才能让它取代灰色和橙色?
谢谢
最佳答案
完全自定义 tabwidget 的步骤:
为 tabwidget 创建自定义布局:此布局类似于默认布局,由一个
Icon
和一个TextView
组成。您可以选择任何您想要的布局。请注意,父布局具有有状态背景tabwidget_selector
。这个可绘制对象是替换默认橙色焦点颜色的关键。您可以选择自己的焦点颜色。<?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
View 的函数。为您的 tabwidget 设置文本、可绘制图标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; }
在您的 Activity 中使用此功能:
TabHost.TabSpec setIndicator(View view) Specify a view as the tab indicator.
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);
关于android - 在eclipse中设置标签背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13469848/