【问题标题】:tabhost /tabbar icons not showingtabhost /tabbar 图标未显示
【发布时间】:2013-02-11 20:46:19
【问题描述】:

我的标签栏图标不会显示

TabHost tabHost = getTabHost();

TabSpec barcodeInsertSpec = tabHost.newTabSpec("Barcode Insert");
barcodeInsertSpec.setIndicator("Barcode Insert", getResources().getDrawable(R.drawable.home2));
barcodeInsertSpec.setContent(new Intent(getBaseContext(), BarcodeInsertActivity.class));

tabHost.addTab(barcodeInsertSpec);

drawable/home2.xml

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- When not selected, use that-->
        <item android:drawable="@drawable/home22" />
    </selector>

并将图片放在三个不同尺寸(48x48、32x32、24x24)的文件夹中drawable-hdpi , drawable-ldpi, ... as .p​​ng

喜欢drawable-hdpi/home22.png

【问题讨论】:

    标签: android xml icons android-tabhost tabbar


    【解决方案1】:

    试试这个代码

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
         <item  android:state_focused="true"
                android:state_pressed="true"
                android:drawable="@drawable/button0_click"></item>
        <item   android:state_focused="true"
                android:state_pressed="false"
                android:drawable="@drawable/button0_click"></item>
        <item
                android:state_focused="false"
                android:state_pressed="true"
                android:drawable="@drawable/button0_click" />
         <item 
                android:drawable="@drawable/button0"></item>
    
    </selector>
    

    【讨论】:

    • 所有按钮看起来都不一样
    【解决方案2】:

    这段代码对我有用:)

    Resources res=getResources();
    TabHost Tabs = (TabHost) findViewById(R.id.your_id);
    Tabs.setup();
    TabHost.TabSpec spec;
    spec = Tabs.newTabSpec("tag1");
    spec.setContent(R.id.tab1);
    spec.setIndicator("Home", res.getDrawable(R.drawable.home_icon));
    Tabs.addTab(spec);
    

    【讨论】:

      【解决方案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/select" />
        <item
          android:state_selected="false"
          android:drawable="@drawable/deselct" />
      </selector>
      

      【讨论】:

        【解决方案4】:

        我在使用 Xamarin 时遇到了同样的问题。

        这是一个显示图标但不显示文本的 hack 解决方案(看起来像 android 缺陷):

        此解决方案同时显示了两者,但它破坏了我的应用程序范围的样式,这表明我可以设置样式覆盖:

        编辑 2013 年 10 月 10 日 - 我让它在 c# 中工作! 在java中做同样的事情应该很简单

        我能够让标签显示带有代码和自定义样式的图标。见下文

        tab_indicator_holo.xml:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="@dimen/tab_host_default_height"
        android:orientation="horizontal"
        style="@style/TabBlood"
        android:gravity="center">
        
        <ImageView
            android:id="@android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:visibility="visible"
            android:contentDescription="@null" />
        
        <TextView
            android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            style="@style/TabTextBlood" />
        
        </LinearLayout>
        

        我的 Activity OnCreate:

        ...
        var spec = this.TabHost.NewTabSpec(tag);
        var drawableIcon = Resources.GetDrawable(drawableId);
        View tabIndicator = LayoutInflater.From(this).Inflate(Resource.Layout.tab_indicator_holo, this.TabHost.TabWidget, false);
        TextView title = tabIndicator.FindViewById<TextView>(Android.Resource.Id.Title);
        ImageView img = tabIndicator.FindViewById<ImageView>(Android.Resource.Id.Icon);
        title.Text = label;
        img.SetImageDrawable(drawableIcon);
        spec.SetIndicator(tabIndicator);
        spec.SetContent(intent);
        this.TabHost.AddTab(spec);
        

        【讨论】:

          【解决方案5】:

          试试这个。

            TabHost th = getTabHost();
            TabHost.TabSpec sp;
            sp=th.newTabSpec("tab 1");
            sp.setIndicator("Home");
            sp.setContent(new Intent(this, Welcome.class));
            th.addTab(sp);
            tabhost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.YOURIMAGE);
          

          *您还可以使用自定义项目选择器代替 drawable 来制作聚焦和按下效果。 干杯!

          【讨论】:

          • 嘿,这可能是一个很好的解决方案 - 但是您能否解释一下您更改了哪些位以及为什么?不要忘记 - 许多新手也来到 Stack Overflow,他们可能没有注意到(对您而言)什么是明显的变化。
          猜你喜欢
          • 2020-08-20
          • 2021-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-23
          • 1970-01-01
          • 2013-12-28
          相关资源
          最近更新 更多