TabHost图标扩展到最大尺寸

时间:2015-07-15 15:54:29

标签: java android xml android-layout android-activity

我使用tabHost遇到了一些问题.. 我试图将5个24x24 png文件插入tabHost,如下所示,但图标扩展到tabhost的最大大小。

这是我的代码:

tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.xxx);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.xxx);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.xxx);
tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.xxx);
tabHost.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.xxx);

我厌倦了这段代码,却无法得到我想要的东西。

tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = (int) (30 * this.getResources().getDisplayMetrics().density);

我得到的是: http://i860.photobucket.com/albums/ab166/season04/Capture_zpsrwblmx7y.png

我想要的是: http://i860.photobucket.com/albums/ab166/season04/Capture_1_zpsvwaeu7gu.png

请帮忙

1 个答案:

答案 0 :(得分:0)

我用这个代码和xml解决了我的问题。 =)

private void addTab(String labelId, int drawableId, Class<?> c, int renewActivity)
    {
        Intent intent = new Intent(this, c);

        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.z_tab_indicator, getTabWidget(), false);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.Tab_icon);
        icon.setImageResource(drawableId);
        spec.setIndicator(tabIndicator);

        switch (renewActivity) {
            case 0:
                spec.setContent(intent);
                break;
            case 1:
                spec.setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                break;
            default:
        }
        tabHost.addTab(spec);
    }

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

    <ImageView android:id="@+id/Tab_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>

如果有人和我有同样的问题。 =)

相关问题