如何更改选项卡主机中的默认图像位置:安卓



我需要更改选项卡主机内图像的默认位置,当我们单击任何选项卡时,我想在每个选项卡的顶部将图像显示为指向图像..那么现在如何更改图像在选项卡小部件旁边的默认位置?我的显示选项卡小部件的简单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"
    android:padding="5dp">
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_weight="1"/>
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:layout_marginBottom="-4dp"/>
</LinearLayout>

您可以使用

setIndicator 将tabwidget膨胀到您想要的任何视图(视图视图)

public TabHost.TabSpec setIndicator (View view)
Since: API Level 4
Specify a view as the tab indicator.

例:

选项卡小组件布局:tab_widget_custom.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/tabwidget_selector"
    android:gravity="center"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/tabIndicatorIcon"
        android:layout_width="28dp"
        android:layout_height="28dp"
        android:layout_marginTop="2dp" />
    <TextView
        android:id="@+id/tabIndicatorText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:textColor="@color/white" />
</LinearLayout>

在活动中创建方法返回tabwidget视图

private View getTabIndicatorView(Context context, String tag, int drawable) {
        View view = LayoutInflater.from(context).inflate(
                R.layout.tab_widget_custom, null);
        TextView tv = (TextView) view.findViewById(R.id.tabIndicatorText);
        tv.setText(tag);
        ImageView tabIndicatorIcon = (ImageView) view
                .findViewById(R.id.tabIndicatorIcon);
        tabIndicatorIcon.setBackgroundResource(drawable);
        return view;
    }

使用此方法创建新tabwidget

private void initTab() {
        Intent intent;
        TabHost.TabSpec spec;
        spec = tabHost.newTabSpec("Inbox").setIndicator(
                getTabIndicatorView(MainTabActivity.this, "Inbox",
                        R.drawable.inbox_tab_selector));
        spec.setContent(intent);
        tabHost.addTab(spec);
        }

最新更新