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
- Android fragment入门一
- Android之Fragment入门一
- Android基础——Fragment基础入门(一)
- Android-Fragment入门用法
- Android fragment入门二
- Android入门之Fragment
- android Fragment入门
- 【Android】Fragment入门
- android Fragment 入门介绍
- android Fragment 入门
- Android Fragment 入门介绍
- Android入门之Fragment
- Android Fragment完全解析,关于碎片你所需知道的一切,Fragment入门(一)
- Android Fragment实践(一)
- android Fragment实现<一>
- Android fragment详解(一 )
- Android Fragment(一)---简介
- Android fragment(一)
- Android中部署自己的su
- 陌陌的新广告能否为约炮神器正名?
- 控制台窗口位置 设置
- HMAC-SHA1 java版
- SystemProperty小结(Android4.2)
- Android fragment入门一
- java读写文件大全!
- tomcat 部署 web
- Java四则运算表达式求解
- android 应用监听输入法按键事件【比如搜索和回车键等】的整个流程分析
- CGlib简单介绍
- 华为android真机测试Eclipse报错info: log device is empty!
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍
- Spring配置触发器