RadioButton实现底部切换的菜单

布局文件的实现代码如下:
LinearLayout 里面有两个布局FrameLayout 和RadioGroup 垂直排列。
FrameLayout里面放置fragment,radiogroup放置可以
切换fragment的按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.gacmy.maintab.MainActivity">

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

        <FrameLayout
            android:id="@+id/fg_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_shouye"
                style="@style/home_tab_bottom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:onClick="onShouye"
                android:text="test1"
                android:checked="false" />

            <RadioButton
                android:id="@+id/rb_kaoqin"
                style="@style/home_tab_bottom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:onClick="onKaoqin"
                android:text="test2"
              />

            <RadioButton
                android:id="@+id/rb_my"
                style="@style/home_tab_bottom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:onClick="onMy"
                android:text="test3"
              />
        </RadioGroup>
    </LinearLayout>

</RelativeLayout>


代码实现如下:
public class MainActivity extends AppCompatActivity {

    private RadioButton rb_menu1;
    private RadioButton rb_menu2;
    private RadioButton rb_menu3;
    private Fragment fragment1;
    private Fragment fragment2;
    private Fragment fragment3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {
        rb_menu1=(RadioButton)findViewById(R.id.rb_shouye);
        rb_menu2=(RadioButton)findViewById(R.id.rb_kaoqin);
        rb_menu3=(RadioButton)findViewById(R.id.rb_my);
    }
    //切换fragment的方法。
    @SuppressLint("NewApi")
    private void setSelection(int index) {
        resetImg();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        hideFragments(ft);
        switch (index) {
            case 0:
                rb_menu1.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_launcher, 0, 0);
                rb_menu1.setTextColor(rb_menu1.getResources().getColor(R.color.colorAccent));
                if (fragment1 == null) {
                    fragment1 = new Fragment();
                    ft.add(R.id.fg_content, fragment1);
                } else {
                    ft.show(fragment1);
                }
                break;
            case 1:

                rb_menu2.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.mipmap.ic_launcher, 0, 0);
                rb_menu2.setTextColor(rb_menu2.getResources().getColor(R.color.colorAccent));
                if (fragment2 == null) {
                    fragment2 = new Fragment();
                    ft.add(R.id.fg_content, fragment2);
                } else {
                    ft.show(fragment2);
                }
                break;
            case 2:
                rb_menu3.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.mipmap.ic_launcher, 0, 0);
                rb_menu3.setTextColor(rb_menu3.getResources().getColor(R.color.colorAccent));
                if (fragment3 == null) {
                    fragment3 = new Fragment();
                    ft.add(R.id.fg_content, fragment3);
                } else {
                    ft.show(fragment3);
                }
                break;
        }
        ft.commit();
    }

    //重置切换布局的按钮
    @SuppressLint("NewApi")
    private void resetImg() {
        rb_menu1.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.drawable.ic_photo_white_24dp, 0, 0);
        rb_menu1.setTextColor(rb_menu1.getResources().getColor(R.color.colorPrimary));

        rb_menu2.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.ic_photo_white_24dp, 0, 0);
        rb_menu2.setTextColor(rb_menu2.getResources().getColor(R.color.colorPrimary));

        rb_menu3.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.ic_photo_white_24dp, 0, 0);
        rb_menu3.setTextColor(rb_menu3.getResources().getColor(R.color.colorPrimary));
    }

    private void hideFragments(FragmentTransaction ft) {
        if (fragment1 != null) {
            ft.hide(fragment1);
        }
        if (fragment2 != null) {
            ft.hide(fragment2);
        }
        if (fragment3 != null) {
            ft.hide(fragment3);
        }
    }

    public void onShouye(View view) {
        setSelection(0);
    }

    public void onKaoqin(View view) {
        setSelection(1);
    }

    public void onMy(View view) {
        setSelection(2);
    }




}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 Python 的 tkinter 模块实现页面切换,可以使用 radiobutton(单选按钮)来控制切换。下面是一个简单的示例代码: ```python import tkinter as tk class PageOne(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.controller = controller label = tk.Label(self, text="Page One") label.pack() class PageTwo(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.controller = controller label = tk.Label(self, text="Page Two") label.pack() class PageThree(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.controller = controller label = tk.Label(self, text="Page Three") label.pack() class MyApp(tk.Tk): def __init__(self): tk.Tk.__init__(self) container = tk.Frame(self) container.pack(side="top", fill="both", expand=True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.pages = {} for Page in (PageOne, PageTwo, PageThree): page = Page(container, self) self.pages[Page] = page page.grid(row=0, column=0, sticky="nsew") self.show_page(PageOne) def show_page(self, page_to_show): page = self.pages[page_to_show] page.tkraise() app = MyApp() app.mainloop() ``` 在这个示例中,我们定义了三个页面类:`PageOne`、`PageTwo` 和 `PageThree`。每个页面类都继承自 `tkinter.Frame`,并具有一个 `controller` 参数,用于在页面之间进行切换。 然后,我们创建了一个继承自 `tkinter.Tk` 的 `MyApp` 类,其中包含了一个用于容纳页面的 `container`。我们使用 `grid` 布局将每个页面放入容器中,并使用 `show_page` 方法来切换页面。 在 `MyApp` 的构造函数中,我们创建了三个页面实例,并将它们保存到 `self.pages` 字典中。我们默认显示第一个页面(即 `PageOne`),但你可以根据需要更改。 你可以运行这段代码来查看页面切换的效果。选择不同的 radiobutton切换到相应的页面。希望对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值