1

我需要更改已设置的 TabHosts 图标的大小。

目前,图标是栏的高度,看起来很可怕,因为图标在栏内太大了:

在此处输入图像描述

我有什么办法可以缩小 TabHost 内的图像大小吗?

主要活动:

public class MainActivity extends FragmentActivity {

/* Tab identifiers */
private final String TAB_A = "Appointments";
private final String TAB_B = "Calender";
private final String TAB_C = "Profile";
private final String TAB_D = "Settings";

private String selectedTab;

private static TabHost mTabHost;

private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private Fragment4 fragment4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    fragment1 = new Fragment1();
    fragment2 = new Fragment2();
    fragment3 = new Fragment3();
    fragment4 = new Fragment4();

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setOnTabChangedListener(listener);
    mTabHost.setup();

    initializeTab();
}

// TABS

public void initializeTab() {
    TabHost.TabSpec spec = mTabHost.newTabSpec(TAB_A);
    mTabHost.setCurrentTab(0);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });
    spec.setIndicator(createTabView(TAB_A, R.drawable.tab1_selector));
    mTabHost.addTab(spec);

    spec = mTabHost.newTabSpec(TAB_B);

    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }

    });

    spec.setIndicator(createTabView(TAB_B, R.drawable.tab2_selector));
    mTabHost.addTab(spec);

    spec = mTabHost.newTabSpec(TAB_C);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });
    spec.setIndicator(createTabView(TAB_C, R.drawable.tab3_selector));

    mTabHost.addTab(spec);

    spec = mTabHost.newTabSpec(TAB_D);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });
    spec.setIndicator(createTabView(TAB_D, R.drawable.tab4_selector));
    mTabHost.addTab(spec);

    mTabHost.setCurrentTab(0);
}

TabHost.OnTabChangeListener listener = new TabHost.OnTabChangeListener() {
    public void onTabChanged(String tabId) {
        /* Set current tab.. */
        if (tabId.equals(TAB_A)) {
            pushFragments(tabId, fragment1);
            selectedTab = TAB_A;
        } else if (tabId.equals(TAB_B)) {
            pushFragments(tabId, fragment2);
            selectedTab = TAB_B;
        } else if (tabId.equals(TAB_C)) {
            pushFragments(tabId, fragment3);
            selectedTab = TAB_C;
        } else if (tabId.equals(TAB_D)) {
            pushFragments(tabId, fragment4);
            selectedTab = TAB_D;
        }
    }
};


public void pushFragments(String tag, Fragment fragment) {

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();

    ft.replace(android.R.id.tabcontent, fragment);
    ft.commit();
}


private View createTabView(final String tabText, final int id) {
    View view = LayoutInflater.from(this).inflate(R.layout.tabs_icon, null);
    ImageView imageViewTabIcon = (ImageView) view
            .findViewById(R.id.tab_icon);
    imageViewTabIcon.setImageDrawable(getResources().getDrawable(id));

    return view;
}

public static void tabfresh() {
    mTabHost.setCurrentTab(0);
}

public static void tabfresh_sal() {
    mTabHost.setCurrentTab(1);
}
}
4

2 回答 2

1

您需要创建tabhost_row.xml以使其膨胀。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@color/white"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgTab"
        android:layout_width="YOUR_WIDTH"
        android:layout_height="YOUR_HEIGHT" />


</FrameLayout>

注意:对于上述ImageView设置的高度和宽度,根据您的要求。

然后在你的MainActivity.java,膨胀tabhost_row.xml如下代码。

public TabSpec setIndicator(Context ctx,TabSpec spec,int viewId,int resId,String name) {
        View v = LayoutInflater.from(ctx).inflate(R.layout.tab_row, null);
        ImageView imgTab = (ImageView) v.findViewById(viewId);
        imgTab.setImageDrawable(getResources().getDrawable(resId));

        return spec.setIndicator(v);
    }

现在,添加您的标签,如下所示,

mTabHost.addTab(setIndicator(this,mTabHost.newTabSpec(Commons.TAG_HOME_CONTAINER_FRAGMENT),R.id.imgTab,R.drawable.home_selector,"Home"),HomeContainerFragment.class,null); 
于 2014-01-29T09:22:55.233 回答
0
 float density = getResources().getDisplayMetrics().density;
    int padding = (int) (5f * density);
    int imageSize = (int) (36 * density);

    TabWidget tabs = tabHost.getTabWidget();
    for (int i = 0; i < tabs.getChildCount(); i++) {
        LinearLayout tab = (LinearLayout) tabs.getChildAt(i);

        AppCompatTextView tv = (AppCompatTextView) tab.getChildAt(1);
        AppCompatImageView iv = (AppCompatImageView) tab.getChildAt(0);

        android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(imageSize, imageSize);
        iv.setLayoutParams(lp);
        iv.setPadding(padding, padding, padding, padding);
    }
于 2016-11-03T16:56:28.393 回答