我的Fragment用法小例子
2015-10-20 12:58
323 查看
**
思路:
**
在MainActivity的布局文件中,用一个LinearLayout作为一个占位符,点击ButtonA就将fragmentA显示,点击ButtonB就将fragmentB显示。
好了上代码:
首先是acitivity_main.xml布局文件:
fragment_a.xml布局文件:
fragment_b.xml布局文件:
AFragment.java
BFragment.java:
MainActivity.java:
思路:
**
在MainActivity的布局文件中,用一个LinearLayout作为一个占位符,点击ButtonA就将fragmentA显示,点击ButtonB就将fragmentB显示。
好了上代码:
首先是acitivity_main.xml布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="vertical" > <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:button="@null" android:checked="true" android:gravity="center_horizontal" android:text="radioButtonA" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:button="@null" android:gravity="center_horizontal" android:text="radioButtonB" /> </RadioGroup> <LinearLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> </LinearLayout>
fragment_a.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30dp" android:text="A" /> </LinearLayout>
fragment_b.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30dp" android:text="B" /> </LinearLayout>
AFragment.java
package com.example.fragmenttest; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class AFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_a, container, false); } }
BFragment.java:
package com.example.fragmenttest; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class BFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_b, container, false); } }
MainActivity.java:
package com.example.fragmenttest; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; public class MainActivity extends FragmentActivity implements OnCheckedChangeListener{ AFragment a_fm; BFragment b_fm; private RadioGroup group; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); a_fm = new AFragment(); b_fm = new BFragment(); group = (RadioGroup)findViewById(R.id.radioGroup); group.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radioButton1: getSupportFragmentManager().beginTransaction().replace(R.id.container, a_fm).commit(); break; case R.id.radioButton2: getSupportFragmentManager().beginTransaction().replace(R.id.container, b_fm).commit(); break; default: break; } } }
相关文章推荐
- 如何优化内存使用
- Unity 日志文件的存放
- POJ ——1852 Ants
- LabVIEW设计模式系列——case结构模拟事件结构
- 推送通知--本地推送通知
- glassfish应用服务器安装配置
- poj2387 Til the Cows Come Home
- 分析集结号棋牌游戏有多安全
- Android源码分析-消息队列和Looper
- 这天在宿舍里玩手机,突然班花给我发来一条短信
- 孙子兵法
- C#与IronScheme交互
- 杂乱
- LeetCode(113) Path Sum II
- LeetCode(113) Path Sum II
- python在linux(anaconda)的图形界面(snack)
- 【AD系列教程】在PCB中加入任意图形
- 加法器(减法器)运算放大电路
- select option
- Python复制目录结构脚本代码分享(转)