Android fragment入门一

来源:互联网 发布:天下3萝莉捏脸数据 编辑:程序博客网 时间:2024/05/22 05:32

fragment简单来说,想当于一个轻量级的activity,它不需要到清单文件中配置。用法也比较简单,fragment是3.0以后才引入的新API,下面的例子要3.0以上才行。

效果图


package com.example.fragment;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
package com.example.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment1, null);}}

package com.example.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment2 extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//填充器,将fragment的xml文件,实例化成对象return inflater.inflate(R.layout.fragment2, null);}}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <fragment        android:id="@+id/fragment1"        android:name="com.example.fragment.Fragment1"        android:layout_width="0dip"        android:layout_height="fill_parent"        android:layout_weight="1" />    <fragment        android:id="@+id/fragment2"        android:name="com.example.fragment.Fragment2"        android:layout_width="0dip"        android:layout_height="fill_parent"        android:layout_weight="1" /></LinearLayout>
fragment1.xml
<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"    android:background="#ff0000" >    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="fragment1" /></RelativeLayout>
fragment2.xml
<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"    android:background="#ffff00" >    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="fragment2" /></RelativeLayout>


横竖屏效果图,只需要改变上面代码中mainActivity一点代码:



package com.example.fragment;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//得到手机屏幕宽高int with = getWindowManager().getDefaultDisplay().getWidth();int height = getWindowManager().getDefaultDisplay().getHeight();Fragment1 fragment1 = new Fragment1();Fragment2 fragment2 = new Fragment2();FragmentManager fm = getFragmentManager();//事务,为了保证在修改界面保证一致性,所以加上事务FragmentTransaction ft = fm.beginTransaction();if(with > height){//横屏的时候     ctrl + f11/12 横竖屏切换ft.replace(android.R.id.content, fragment1);}else{ft.replace(android.R.id.content, fragment2);}ft.commit();}}



0 0
原创粉丝点击