fragment 的简单用法
来源:互联网 发布:oracle数据库日志清理 编辑:程序博客网 时间:2024/06/04 18:48
1.left_fragment.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:orientation="vertical" android:background="#00fff0"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="button"/></LinearLayout>2..right_fragment.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:orientation="vertical" android:background="#00ff00"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is right fragment" android:textSize="20sp" android:layout_gravity="center_horizontal"/></LinearLayout>3. LeftFragment.class 新建一个类
public class LeftFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fragment,container,false); return view; }}
4.RightFragment.class 新建一个类
<?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:orientation="vertical" android:background="#00ff00"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is right fragment" android:textSize="20sp" android:layout_gravity="center_horizontal"/></LinearLayout>
5.activity_main.xml 主布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/activity_main" 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" android:orientation="horizontal" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.sh.fragmenttest.MainActivity"> <fragment android:id="@+id/left_fragment" android:name="com.sh.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <FrameLayout android:id="@+id/right_fragment" android:name="com.sh.fragmenttest.RightFragment" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"> </FrameLayout></LinearLayout>
6.another_right_fragment.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:orientation="vertical" android:background="#ff0000"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is another right fragment" android:textSize="20sp" android:layout_gravity="center_horizontal"/></LinearLayout>7.AnotherRightFragment.class 新建一个类
public class AnotherRightFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.another_right_fragment,container,false); return view; }}8.MainAcivity.class 主文件
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(this); replaceFragment(new RightFragment()); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: replaceFragment(new AnotherRightFragment()); default: break; } } private void replaceFragment(Fragment fragment) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.right_fragment,fragment);
transaction.addToBackStack(null);
transaction.commit(); }}
阅读全文
0 0
- Fragment的简单用法
- Fragment的简单用法
- fragment 的简单用法
- Fragment专辑(二):Fragment的简单用法
- Android fragment的用法
- fragment的用法笔记
- -----fragment的用法
- fragment的用法笔记
- fragment的用法笔记
- Fragment的用法
- Fragment的部分用法
- 关于Fragment的用法
- Fragment的用法
- Fragment的用法简介
- Fragment的用法
- Fragment的初步用法
- fragment的基本用法
- fragment回退栈的用法
- 直线和圆位置关系教案免费下载
- 基于HTML5 Canvas 实现弹出框
- java.lang.NoClassDefFoundError: org/apache/commons/lang/xwork/StringUtils
- 20170605Link
- WPF 读取xaml文档
- fragment 的简单用法
- css3正方体
- 欢迎使用CSDN-markdown编辑器
- HashMap
- Spring application和BeanFactory的区别
- 内存堆栈结构
- 集合类
- Android svn的使用
- Spring Data JPA