android - 在eclipse中设置标签背景

标签 android eclipse tabs

我正在制作一个 android 应用程序,我的 xml 中有一个 tabhost 和两个选项卡。我给他们打过电话,他们工作得很好。但我想摆脱标签上丑陋的灰色/橙色。我尝试使用图像设置背景。我使用了这段代码:

th.getTabWidget().setBackgroundResource(R.drawable.tab_normal);

但它是这样显示的:

Wrong bg

我怎样才能让它取代灰色和橙色?

谢谢

最佳答案

完全自定义 tabwidget 的步骤:

  1. 为 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" />
    

  2. 编写返回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;
    
    }
    
  3. 在您的 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/

相关文章:

Android主项目与库项目-如何在项目之间传递设置

android - 如何在 Firestore 中为 Android 创建本地化数据方案(按语言)?

java - Eclipse:如何使用具有外部项目依赖项的 ant 构建可执行 jar?

css - Foundation for Apps 元素中的对齐选项卡

swift - 直接从 UITabBarController 和通过 UINavigationController 选择 UIViewController

android - 命令“phonegap build android”失败

android - 表格行划分

java - 从 Eclipse 项目资源管理器中的 Java 文件和包中删除警告图标

android - Android 4.3 更新后目标未知

jQuery 从 URL 设置事件选项卡