如何正确对齐TabHost的背景图像?

时间:2013-12-12 09:09:49

标签: android android-tabhost

我正在使用Tabhost,我想设置背景图像。但是在设置之后,它就像这样

![在此处输入图片说明] [1]

我如何完全设置它?以下是代码:

/* Tabs */
        Bundle bundle = getIntent().getExtras();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        // First Activity
        intent = new Intent().setClass(this, InfoListView.class);
        //spec = tabHost.newTabSpec("some_things").setIndicator("Info").setContent(intent);
        spec = tabHost.newTabSpec("top_things").setIndicator("Info",getResources().getDrawable(R.drawable.tabicon)).setContent(intent);  
        tabHost.addTab(spec);

        // Second Activity
        intent = new Intent().setClass(this, LogListView.class);
        /*mContext = getApplicationContext();
        TextView mTv = new TextView(mContext);
        mTv.setText("Sync Log");
        mTv.setBackgroundDrawable(mContext.getResources.getDrawable(R.drawable.tabicon));
        spec = tabHost.newTabSpec("top_things").setIndicator(mTv).setContent(intent);*/
        spec = tabHost.newTabSpec("top_things").setIndicator("Sync Log", getResources().getDrawable(R.drawable.tabicon)).setContent(intent);  
        tabHost.addTab(spec);

        tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 95;
        tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 95;

/ *标签结束* /

XML

<TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/title" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="0dp" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true" />

            <View
                android:id="@+id/separator"
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:layout_below="@android:id/tabs" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_above="@+id/btnSend"
                android:layout_below="@+id/separator"
                android:paddingBottom="45dip" >

                <!-- Scrollview for message data -->

                <ScrollView
                    android:id="@+id/formTab"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:scrollbars="vertical" >

                    <LinearLayout
                        android:id="@+id/formLayout"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical" >

                        <View
                            android:layout_width="fill_parent"
                            android:layout_height="5dip" />
                    </LinearLayout>
                </ScrollView>
            </FrameLayout>
        </RelativeLayout>
    </TabHost>

3 个答案:

答案 0 :(得分:0)

在您添加图片的部分的xml中添加此代码android:gravity="center"android:layout_gravity="center"

或添加 -

spec.setGravity(Gravity.CENTER);

在添加之前。

答案 1 :(得分:0)

使用指标初始化tabhost之后,其他内容就像这样添加背景

 tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabicon); //fro first tab 
 tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabicon); //for second tab           

答案 2 :(得分:0)

我得到了答案:可以这样做:

TabHost tabHost = getTabHost();  // The activity TabHost
               TabHost.TabSpec spec;  // Resusable TabSpec for each tab
               Intent intent;  // Reusable Intent for each tab
               // Create an Intent to launch an Activity for the tab (to be reused)
               intent = new Intent().setClass(this, InboxActivity.class);
               // Create our custom view. 
               View tabView = createTabView(this, "Inbox");
               // Initialize a TabSpec for each tab and add it to the TabHost
               spec = tabHost.newTabSpec("Inbox").setIndicator(tabView)
                       .setContent(intent);
               tabHost.addTab(spec);
               // Do the same for the other tabs
               tabView = createTabView(this, "Outbox");
               intent = new Intent().setClass(this, OutboxActivity.class);
               spec = tabHost.newTabSpec("Outbox").setIndicator(tabView)
                       .setContent(intent);
               tabHost.addTab(spec);

方法

private static View createTabView(Context context, String tabText) {
        View view = LayoutInflater.from(context).inflate(R.layout.custom_tab, null, false);
        TextView tv = (TextView) view.findViewById(R.id.tabTitle);
        tv.setText(tabText);
        return view;
    }

Custom_tab

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/tabTitle"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center_horizontal"
          android:clickable="true"
          android:padding="5dp"
          android:textSize="15sp"
          android:textStyle="bold"
          android:textColor="#FFFFFF"
          android:background="@drawable/tab_selector"/>

tab_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:state_pressed="false" 
          android:drawable="@drawable/tabbackground" />
    <item android:state_selected="false" android:state_pressed="false" 
          android:drawable="@drawable/tabnormal" />
    <item android:state_pressed="true" 
          android:drawable="@drawable/tabbackground" />
</selector>