标签和图像的一些设备 [英] tabs and images with some devices

查看:188
本文介绍了标签和图像的一些设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的一部分延伸FragmentActivity。

Part of my app extends FragmentActivity.

该标签具有图像和文本(它有3个标签,但他们也有类似的code,唯一的区别是文本和图像):

The tab has an image and a text (it has 3 tabs, but they has similar code, the only difference is the text and the image):

 mTabManager.addTab("map", mTabHost.newTabSpec("map").setIndicator(context.getString(R.string.map), activity.getResources().getDrawable(R.drawable.maps_tab)), Maps.class, bundlePos);

和maps_tab.xml:

And maps_tab.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use icon1 -->
<item android:drawable="@drawable/ic_mapa_selected"
  android:state_selected="true" />
<!-- When not selected, use icon2-->
<item android:drawable="@drawable/ic_mapa_unselected" />
</selector>

好这项工作很好的一些设备如银河王牌,还是HTC野火S:

Ok this work fine in some devices as Galaxy Ace, or HTC Wildfire S:

但没有其它设备,作为GALAXY SII,银河SIII或HTC Desire的ç

But fails in other devices as Galaxy SII, Galaxy SIII or HTC Desire C

这怎么能不兼容解决?

推荐答案

下面是实现同一

TabSample.java

public class TabSample extends TabActivity {
    /** Called when the activity is first created. */   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTabs() ;
    }
    private void setTabs()
    {
        addTab("Home", R.drawable.tab_home, ArrowsActivity.class);
        addTab("Search", R.drawable.tab_search, OptionsActivity.class);

        //To add more tabs just use addTab() method here like previous line.
    }

    private void addTab(String labelId, int drawableId, Class<?> c)
    {
        TabHost tabHost = getTabHost();
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

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

        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
        int i = tabHost.getCurrentTab();
        Toast.makeText(TabSample.this, "tab->" + i,Toast.LENGTH_SHORT).show();
    }
}

的main.xml

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="0dip"
            android:layout_weight="1" />

        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_weight="0"  />
    </LinearLayout>
</TabHost> 

tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip"
    android:layout_height="55dip"    
    android:layout_weight="1"
    android:orientation="vertical"

    android:background="@drawable/tab_indicator"
    android:padding="5dp">

    <ImageView android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/icon"

    /> 
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" 
        android:layout_centerHorizontal="true"
        style="?android:attr/tabWidgetStyle"
    />    
</RelativeLayout>

让我知道,如果你有任何问题,这
谢谢

Let me know if you have any problem with this
Thanks

这篇关于标签和图像的一些设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆