Android Tab Widget won't fill the width of the

2019-09-17 20:30发布

I'm completely new to Android dev and XML. What I'm trying to do is create 4 tabs at the bottom of the screen, and for the tabs to fill the width of the screen. I have been able to create the 4 tabs at the bottom, but even though the width's are all set to "fill_parent", the tab widget doesn't fill the width. However, in the graphical layout in Eclipse, it shows the widget filling the width. Can anyone help? Code is below. Thanks.

<?xml version="1.0" encoding="utf-8"?>
<!-- Dublin Bus App -->

<TabHost   
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab_host">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:id="@+id/tab1"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="5px" >

            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="This is the first tab" 
                android:textSize="25dp"
                android:layout_gravity="center"
                android:textStyle="bold"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab2"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5px" > 

            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="This is the second tab" 
                android:layout_gravity="center"
                android:textStyle="bold" 
                android:textSize="25dp"/>
            </LinearLayout>

        <LinearLayout
            android:id="@+id/tab3"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5px"> 

            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="This is the third tab" 
                android:layout_gravity="center"
                android:textStyle="bold" 
                android:textSize="25dp"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab4"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5px"> 

            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="This is the fourth tab" 
                android:layout_gravity="center"
                android:textStyle="bold" 
                android:textSize="25dp"/>
        </LinearLayout>             
    </FrameLayout>
</TabHost>

2条回答
萌系小妹纸
2楼-- · 2019-09-17 20:41

Try android:width="match_parent"

查看更多
冷血范
3楼-- · 2019-09-17 21:07

Your problem is that TabHost is derived from FrameLayout, which, as the description states, is only for holding one element, and you're stuffing it with two: a TabWidget and a FrameLayout.

Try this: (note how I also removed FrameLayout - trying to fill in tabs like that is a bad idea)

<?xml version="1.0" encoding="utf-8"?>
<!-- Dublin Bus App -->

<TabHost   
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab_host">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>

Then follow the HelloTabWidget tutorial to fill the tabs with content.

查看更多
登录 后发表回答