Android fragment入门一
2014-05-08 10:22
323 查看
fragment简单来说,想当于一个轻量级的activity,它不需要到清单文件中配置。用法也比较简单,fragment是3.0以后才引入的新API,下面的例子要3.0以上才行。
效果图
横竖屏效果图,只需要改变上面代码中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 {
@Override
protected 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();
}
}
效果图
package com.example.fragment; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected 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{ @Override public 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{ @Override public 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 {
@Override
protected 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();
}
}
相关文章推荐
- Android零基础入门第89节:Fragment回退栈及弹出方法
- Android fragment入门二
- Android零基础入门第86节:探究Fragment生命周期
- 11_android入门_Fragment的生命周期
- Android app开发中的Fragment入门学习教程
- Android入门之Fragment用法
- Android入门之Fragment用法
- Android入门之Fragment简介
- Android Fragment完全解析,关于碎片你所需知道的一切,Fragment入门(一)
- Android基础入门教程——5.2.5 Fragment实例精讲——新闻(购物)类App列表Fragment的简单实现
- Android基础——Fragment基础入门(一)
- [原]Android Fragment 入门介绍
- Android零基础入门第87节:Fragment添加、删除、替换
- 【Android基础入门〖11〗】Fragment
- Android Fragment 入门介绍
- Android零基础入门第84节:引入Fragment原来是这么回事
- Android零基础入门第88节:Fragment显示和隐藏、绑定和解绑
- Android入门笔记之Fragment
- Android笔记 fragment入门 静态加载fragment
- Android中Fragment入门概念