在FragmentTabHost中设置非矩形图像背景

时间:2015-03-09 06:38:28

标签: android fragment-tab-host

我需要在FragmentTabHost背景图片中设置非矩形或方形图像,如下所示:http://i.imgur.com/yWbDi0a.jpg?1

但是我得到了这个:http://i.imgur.com/g1jvmsb.png?1(标签之间的默认分隔符正在切割我的图像):(请帮忙

我有这些对角形状的图像,我想在我的各个标签中将它们设置为背景图像。这就是我迄今所做的。

tab_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>

<ImageView 
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal" />

</LinearLayout>

MainActivity.java
public class MainActivity extends ActionBarActivity {
private FragmentTabHost mTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);

    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator(getTabIndicator(mTabHost.getContext(),  R.drawable.tab1)),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator(getTabIndicator(mTabHost.getContext(),  R.drawable.tab2)),
            FragmentTab.class, null);

}

private View getTabIndicator(Context context, int icon) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
    ImageView iv = (ImageView) view.findViewById(R.id.imageView);
    iv.setImageResource(icon);
    return view;
}
}

1 个答案:

答案 0 :(得分:0)

您可以使用TabWidget并通过设置分隔线颜色使分隔线透明

android:divider="@color/transparent"

你的TabHost xml可以是这样的 -

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/card_background" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:paddingTop="@dimen/padding_small"
            android:divider="@color/transparent"
            android:background="@drawable/footer_tab_widget_background" />
    </LinearLayout>
    <!-- "-->
</android.support.v4.app.FragmentTabHost>

并且TabHost和TabWidget设置的代码可以像这样

mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
        mTabWidget = (TabWidget) rootView.findViewById(android.R.id.tabs);
        mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);