Tab中的图标未显示

the*_*oly 20 android

我开始使用Android,当我尝试使用图标和文本进行TabHost时.只有文本可见,如果我将文本留空,则可以看到图标.我想在屏幕上看到两个.

有人可以给我建议吗?

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Usuario usu = new Usuario();
usu.nombre = "fsdf";
lista.add(usu);

adaptador = new AdaptadorCelda(this,lista);
ListView lstView = (ListView)findViewById(R.id.lista);
lstView.setAdapter(adaptador);

Button btn = (Button)findViewById(R.id.btnSalva);

btn.setOnClickListener(onSave);



Resources res = getResources();

TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();

TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
spec.setContent(R.id.tab1);
spec.setIndicator("",res.getDrawable(android.R.drawable.ic_menu_rotate));


tabs.addTab(spec);

spec=tabs.newTabSpec("mitab2");
spec.setContent(R.id.tab2);
spec.setIndicator("ddd",
       res.getDrawable(android.R.drawable.presence_invisible));
tabs.addTab(spec);

tabs.setCurrentTab(0);

spec.setIndicator("",res.getDrawable(android.R.drawable.ic_menu_rotate)) In this case icon appears.

spec.setIndicator("ddd",
       res.getDrawable(android.R.drawable.presence_invisible));
Run Code Online (Sandbox Code Playgroud)

这是main.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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" >

<TabWidget
    android:id="@android:id/tabs"
    android:layout_width="match_parent"

    android:layout_height="wrap_content" />

<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tab1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
       <TextView
            android:id="@+id/lblNombre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="17dp"
            android:text="Nombre"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/txtNombre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/lblNombre"
            android:layout_marginLeft="35dp"
            android:layout_toRightOf="@+id/lblNombre"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/txtApellido"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/txtNombre"
            android:layout_below="@+id/txtNombre"
            android:ems="10" />

        <TextView
            android:id="@+id/lblApellido"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/txtApellido"
            android:layout_alignParentLeft="true"
            android:text="Apellidos"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <RadioGroup
            android:id="@+id/tipos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txtApellido"
            android:layout_marginTop="35dp"
            android:layout_toLeftOf="@+id/txtApellido" >

            <RadioButton
                android:id="@+id/rdbAlta"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="alta" />

            <RadioButton
                android:id="@+id/rdbBaja"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="baja" />

            <RadioButton
                android:id="@+id/rdbTramite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="tramite" />
        </RadioGroup>

        <Button
            android:id="@+id/btnSalva"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/txtApellido"
            android:layout_alignTop="@+id/tipos"
            android:layout_marginLeft="58dp"
            android:layout_marginTop="30dp"
            android:text="Button" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/txtApellido"
            android:src="@drawable/ic_menu_chart" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/tab2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/lista"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/tipos"
            android:layout_marginTop="24dp" >
        </ListView>
    </LinearLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

sun*_*rer 53

您是否在运行Ice Cream Sandwich的设备上测试了您的应用程序?我最近注意到,默认情况TabHost会有所不同,具体取决于目标设备和Android平台版本.

运行提供图标和文本的应用程序setIndicator(如问题的来源),具有以下测试结果:

  • 使用Android 2.1的手机:显示的图标和文字(图标下方的标签符合预期)
  • 手机4.0.3:图标没有显示,只有文字可见.
  • 运行ICS的平板电脑:显示图标和文字的标签

正如您所发现的那样,用空字符串替换标签,电话上出现图标 - 但是用户需要猜出图标的含义.除此之外:在前ICS手机上运行此版本的应用程序:标签保持其大小并在图像下方留下空白区域.

要更改此设备驱动决策选项卡应该获得多少空间,我在本教程中找到了有关如何自定义选项卡的解决方案:

首先,您需要提供自己的选项卡指示器布局(教程中的"layout/tab_indicator.xml"),包括a ImageView和a TextView.然后你告诉TabSpec使用这个通过setIndicator.瞧:你也可以通过ICS获得手机上的图标和标签.

以下是关于如何设置指标的重要部分(这=当前活动):

TabHost.TabSpec spec = this.getTabHost().newTabSpec(tag);

View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
((TextView) tabIndicator.findViewById(R.id.title)).setText(label);
((ImageView) tabIndicator.findViewById(R.id.icon)).setImageResource(drawableResourceId);

spec.setIndicator(tabIndicator);
Run Code Online (Sandbox Code Playgroud)

如果您喜欢旧款和新款手机上的标签外观相同,请点击此处:http: //jgilfelt.github.com/android-actionbarstylegenerator/.此页面生成图像和样式以自定义操作栏,包括选项卡,并帮助我弄清楚如何定义9补丁背景图像.

  • 该教程似乎处于脱机状态,但仍处于Internet Archive:http://web.archive.org/web/20120925030428/http://ondrejcermak.info/programovani/custom-tabs-in-android-tutorial/ (6认同)
  • 太棒了,你应该做教程.真正易于阅读和理解. (2认同)
  • 选择选项卡时,此方法还会删除图像/文本下的"细线". (2认同)
  • 你知道为什么android工程师对开发者有这样的好处吗? (2认同)

Jav*_*aez 9

这是在ICS中显示文本和图标的简便方法.在设置Tabhost之后,我调用下一个方法:

setTabIcon(mTabHost, 0, R.drawable.ic_tab1); //for Tab 1
setTabIcon(mTabHost, 1, R.drawable.ic_tab2); //for Tab 2

public void setTabIcon(TabHost tabHost, int tabIndex, int iconResource) {
    ImageView tabImageView = (ImageView) tabHost.getTabWidget().getChildTabViewAt(tabIndex).findViewById(android.R.id.icon);
    tabImageView.setVisibility(View.VISIBLE);
    tabImageView.setImageResource(iconResource);
}
Run Code Online (Sandbox Code Playgroud)

如果您想要更高级的解决方案,则需要创建像@sunadorer推荐的布局.我的建议是你可以使用holo主题布局创建你的布局,在你的sdk_dir/platforms/android-14/data/res/layout/tab_indicator_holo.xml上搜索文件.

我的高级解决方案是下一个,您需要创建这样的布局:

tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:paddingTop="5dp"
              android:paddingBottom="5dp"
              style="@android:style/Widget.Holo.Tab">

    <ImageView
        android:id="@android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:visibility="visible" />

    <TextView
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        style="@android:style/Widget.Holo.ActionBar.TabText" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在您的活动或片段中,创建下一个函数:

public View createTabIndicator(LayoutInflater inflater, TabHost tabHost, int textResource, int iconResource) {
    View tabIndicator = inflater.inflate(R.layout.tab_indicator, tabHost.getTabWidget(), false);
    ((TextView) tabIndicator.findViewById(android.R.id.title)).setText(textResource);
    ((ImageView) tabIndicator.findViewById(android.R.id.icon)).setImageResource(iconResource);
    return tabIndicator;
}
Run Code Online (Sandbox Code Playgroud)

最后,当您在活动或片段中创建选项卡时,请使用下一个代码:

mTabHost.addTab(
    mTabHost.newTabSpec("tab1")
    .setIndicator(createTabIndicator(inflater, mTabHost, R.string.tab1, R.drawable.ic_tab1))
    .setContent(R.id.tab1)
);
Run Code Online (Sandbox Code Playgroud)

我已经在ICS中测试了这个解决方案并且工作正常.

祝好运 :)


And*_*yeu 6

还有另一个非常简单的解决方案.

似乎问题出现在新的Holo主题和TabWidget.所以你应该做的是在你的values-v11文件夹中定义Holo主题但是使用这样的旧TabWidget样式:

<style name="MyAppTheme" parent="@android:style/Theme.Holo.NoActionBar">
    <item name="android:tabWidgetStyle">@android:style/Widget.TabWidget</item>
</style>
Run Code Online (Sandbox Code Playgroud)

它适用于我,希望它可以帮助某人.