美文网首页
Frament 介绍

Frament 介绍

作者: Summer_27d1 | 来源:发表于2018-05-31 11:38 被阅读0次

碎片 :
首先创建一个项目
创建一个包 里面是Fragment类

一,首先在mainActivity.xml 中 定义一个碎片
name属性指向你要加载的Fragment

这里我们定义一个FrameLayout 标签 里面有两个按钮 切换界面用

main.xml -----------------
代码如下
···
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/main_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00ff00"
></FrameLayout>
<LinearLayout
android:id="@+id/main_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#0000ff"

 >
 <Button 
     android:id="@+id/bt_1"
     android:layout_width="0dp"
     android:layout_height="40dp"
     android:layout_weight="1"
     android:text="首页"
     
     />
  <Button 
     android:id="@+id/bt_2"
     android:layout_width="0dp"
     android:layout_height="40dp"
     android:layout_weight="1"
     android:text="购物"
     
     />

</LinearLayout>
</LinearLayout>
···
在创建两个类继承Fragment

*************首页*********
···
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_index, null);
return view;
}
···
两个布局
fragment_indext -----------
···
<?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="#fff">
<TextView

android:layout_width="match_parent"
 android:layout_height="match_parent"
    android:text="这是首页"
    />

</LinearLayout>

···
第二个布局同上

开始在MainActivity 中写代码
···
public class MainActivity extends Activity {
FragmentManager fm;
Fragment2 fragment2;
FragmentShopping fragmentShopping;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.创建Fragment管理者
fm = getFragmentManager();
//创建碎片
fragment2 = new Fragment2();
fragmentShopping = new FragmentShopping();

         //首页
    findViewById(R.id.bt_1).setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        //2.开启事务
        FragmentTransaction fb = fm.beginTransaction();
        //3.替换碎片
        fb.replace(R.id.main_1, fragment2);
        //4.提交事务
        fb.commit();
    
        
    
    }
});
    //购物
findViewById(R.id.bt_2).setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        //2.开启事务
        FragmentTransaction fb = fm.beginTransaction();
        //3.替换碎片
        fb.replace(R.id.main_1, fragmentShopping);
        //4.提交事务
        fb.commit();
    
        
    }
});
    
    
}

}

···
效果图**************


image.png
image.png

相关文章

网友评论

      本文标题:Frament 介绍

      本文链接:https://www.haomeiwen.com/subject/oucqsftx.html