在ANDROID中使用片段来获取TAB功能

时间:2013-10-12 04:55:38

标签: android android-fragments

使用带片段的单选按钮实现选项卡功能

我使用tabhost ...常规标签获得此功能。

enter image description here


  • 是否可以使用片段来实现此功能,如图
  • 所示

如果是这样的话?

  • 示例会有所帮助

1 个答案:

答案 0 :(得分:-1)

试试如下:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // radiobutton to change Fragment
        RadioButton tab1 = (RadioButton) findViewById(R.id.radioFemale);
        RadioButton tab2 = (RadioButton) findViewById(R.id.radioMale);

        RadioGroup tab_group = (RadioGroup) findViewById(R.id.radioSex);
        // radioGroupListener
        tab_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                case R.id.radioFemale:
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction ft1 = fm.beginTransaction();
                    Fragment m_fragSet = new GamesFragment();
                    ft1.replace(R.id.frameLayout2, m_fragSet);
                    ft1.commit();
                    break;
                case R.id.radioMale:
                                    FragmentManager fmg = getFragmentManager();
                FragmentTransaction ftr = fmg.beginTransaction();
                Fragment m_frag = new MoviesFragment();
                ftr.replace(R.id.frameLayout2, m_frag);
                ftr.commit();
                    break;

                default:
                    break;

                }
            }
        });

    }

布局 activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioGroup
            android:id="@+id/radioSex"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radioMale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="radiobutton1" />

            <RadioButton
                android:id="@+id/radioFemale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="radiobutton2" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/passenger"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="2dp"
        android:orientation="horizontal"
        android:weightSum="1" >

        <FrameLayout
            android:id="@+id/frameLayout2"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray" >
        </FrameLayout>
    </LinearLayout>

</LinearLayout>