使Android RadioGroup可滚动到tabhost中

时间:2015-11-10 08:22:12

标签: android scrollview android-tabhost radio-group android-radiogroup

我有一个TabActivity,布局文件如下,我在radiogroup中有选项卡选项,我想根据显示尺寸设置单选按钮的大小,并使radiogroup可滚动。

<?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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >


            <RadioGroup
                android:id="@+id/radio_group"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:orientation="horizontal" 
               >

                <RadioButton
                    android:id="@+id/radio_sale"
                    style="@style/main_tab_bottom"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/tabs_bg_imag_selector"
                    android:checked="true"
                    android:drawableTop="@drawable/selector_sale_tab_img"
                    android:paddingTop="5dp"
                    android:text="@string/sale"
                    android:textSize="12sp" />


                <RadioButton
                    android:id="@+id/radio_bill"
                    style="@style/main_tab_bottom"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/tabs_bg_imag_selector"
                    android:drawableTop="@drawable/selector_bill_tab_img"
                    android:paddingTop="5dp"
                    android:text="@string/billpay"
                    android:textSize="12sp" />

                  <RadioButton
                    android:id="@+id/radio_charge"
                    style="@style/main_tab_bottom"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/tabs_bg_imag_selector"
                    android:drawableTop="@drawable/selector_charge_tab_img"
                    android:paddingTop="5dp"
                    android:text="@string/charge"
                    android:textSize="12sp" />

                 <RadioButton
                    android:id="@+id/radio_balance"
                    style="@style/main_tab_bottom"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/tabs_bg_imag_selector"
                    android:drawableTop="@drawable/selector_balance_tab_img"
                    android:paddingTop="5dp"
                    android:text="@string/balance"
                    android:textSize="12sp" />


                    <RadioButton
                    android:id="@+id/radio_report"
                    style="@style/main_tab_bottom"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/tabs_bg_imag_selector"
                    android:drawableTop="@drawable/selector_report_tab_img"
                    android:paddingTop="5dp"
                    android:text="@string/report"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/radio_config"
                    style="@style/main_tab_bottom"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/tabs_bg_imag_selector"
                    android:drawableTop="@drawable/selector_config_tab_img"
                    android:paddingTop="5dp"
                    android:text="@string/config"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/radio_about"
                    style="@style/main_tab_bottom"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/tabs_bg_imag_selector"
                    android:drawableTop="@drawable/selector_about_tab_img"
                    android:paddingTop="5dp"
                    android:text="@string/about"
                    android:textSize="12sp" />
                  <RadioButton
                    android:id="@+id/radio_about"
                    style="@style/main_tab_bottom"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/tabs_bg_imag_selector"
                    android:drawableTop="@drawable/selector_about_tab_img"
                    android:paddingTop="5dp"
                    android:text="@string/about"
                    android:textSize="12sp" />

            </RadioGroup>


        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >
        </FrameLayout>
     <HorizontalScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:fillViewport="false"
                android:scrollbars="horizontal" >
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#808080"
            android:visibility="gone" >

        </TabWidget>
           </HorizontalScrollView>
    </LinearLayout>

</TabHost>

2 个答案:

答案 0 :(得分:1)

我自己尝试过这段代码并且有效。 只需用这样的滚动视图包装您的广播组:

    <?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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal"

        >

        <RadioButton
            android:id="@+id/radio_sale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:paddingTop="5dp"
            android:text="sale"
            android:textSize="20sp" />


        <RadioButton
            android:id="@+id/radio_bill"
            style="main_tab_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:text="billpay"
            android:textSize="20sp" />

        <RadioButton
            android:id="@+id/radio_charge"
            style="main_tab_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:text="charge"
            android:textSize="20sp" />

        <RadioButton
            android:id="@+id/radio_balance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:text="balance"
            android:textSize="20sp" />


        <RadioButton
            android:id="@+id/radio_report"
            style="main_tab_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:text="report"
            android:textSize="20sp" />

        <RadioButton
            android:id="@+id/radio_config"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:text="config"
            android:textSize="20sp" />

        <RadioButton
            android:id="@+id/radio_about2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:text="about"
            android:textSize="20sp" />
        <RadioButton
            android:id="@+id/radio_about"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:text="about"
            android:textSize="20sp" />

    </RadioGroup>

</HorizontalScrollView>
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>
    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="false"
        android:scrollbars="horizontal" >
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#808080"
            android:visibility="gone" >

        </TabWidget>
    </HorizontalScrollView>
</LinearLayout>

编辑: 对于文本大小,请检查此代码:

int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
        switch (screenSize) {
            case Configuration.SCREENLAYOUT_SIZE_XLARGE:
                //set text size
                break;
            case Configuration.SCREENLAYOUT_SIZE_LARGE:
                //set text size
                break;
            case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                //set text size
                break;
            case Configuration.SCREENLAYOUT_SIZE_SMALL:
                //set text size
                break;

            default:
                //default 

        }

答案 1 :(得分:1)

RadioGroup继承自LinearLayout,它本身不可滚动。我认为你可以将你的RaidoGroup包装在Horizo​​ntalScrollView中,就像正常的LinearLayout do.Hope它可以帮助你