Android-Fragment

1.fragment介绍

fragment总是被嵌入到Activity里 fragment要添加到ViewGroup里
fragment是在android3.0的时候被引入的.
代码实现
1.在布局声明
 
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <fragment android:name="itheima.day12.Test1Fragment"
  7. android:id="@+id/list"
  8. android:layout_weight="1"
  9. android:layout_width="0dp"
  10. android:layout_height="match_parent" />
  11. <fragment android:name="itheima.day12.Test2Fragment"
  12. android:id="@+id/viewer"
  13. android:layout_weight="2"
  14. android:layout_width="0dp"
  15. android:layout_height="match_parent" />
  16. </LinearLayout>
 2.声明fragment 需要重写onCreateView方法
 
 
  1. //创建了一个fragment 代表一个页面
  2. public class Test1Fragment extends Fragment {
  3. //通过下面这个方法加载布局
  4. @Override
  5. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  6. //加载布局 通过打气筒加载
  7. return inflater.inflate(R.layout.fragment_test1, null);
  8. }
  9. }

2.动态添加fragment

 
 
  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. //1.获取手机宽高
  7. WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
  8. int width = wm.getDefaultDisplay().getWidth();
  9. int height = wm.getDefaultDisplay().getHeight();
  10. //2.获取一个fragment的管理者
  11. FragmentManager fragmentManager = getFragmentManager();
  12. //2.1 开启fragment事务
  13. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  14. //3.判断手机状态是横屏还是竖屏
  15. if (height > width){
  16. //说明是竖屏 加载一个页面 参数1:android.代表系统定义好的一些id 理解成是当前手机窗口
  17. fragmentTransaction.replace(android.R.id.content,new Test1Fragment());
  18. }else{
  19. //说明是横屏 加载另外一个页面
  20. fragmentTransaction.replace(android.R.id.content,new Test2Fragment());
  21. }
  22. //4.最后一步 记得提交事务
  23. fragmentTransaction.commit();
  24. }
  25. }

3.通过v4包中的fragment支持低版本.

  兼容低版本就是使用V4包中的fragment .
  注意地方:在获取fragm管理者的方式不一样了 
 
 
  1. FragmentManager supportFragmentManager = getSupportFragmentManager();

4.通过fragment模拟一个微信主页面 

   1.在布局中声明UI
 
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="itheima.wechat.MainActivity">
  8. <LinearLayout
  9. android:id="@+id/ll"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:orientation="vertical">
  13. </LinearLayout>
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentBottom="true"
  18. android:orientation="horizontal">
  19. <Button
  20. android:id="@+id/btn_wx"
  21. android:layout_width="0dp"
  22. android:layout_height="wrap_content"
  23. android:layout_weight="1"
  24. android:text="消息" />
  25. <Button
  26. android:id="@+id/btn_contact"
  27. android:layout_width="0dp"
  28. android:layout_height="wrap_content"
  29. android:layout_weight="1"
  30. android:text="联系人" />
  31. <Button
  32. android:id="@+id/btn_discover"
  33. android:layout_width="0dp"
  34. android:layout_height="wrap_content"
  35. android:layout_weight="1"
  36. android:text="发现" />
  37. <Button
  38. android:id="@+id/btn_mine"
  39. android:layout_width="0dp"
  40. android:layout_height="wrap_content"
  41. android:layout_weight="1"
  42. android:text="我" />
  43. </LinearLayout>
  44. </RelativeLayout>
 2.声明四个fragment,每个fragmen分别代表一个页面 
 3.根据UI写对应逻辑
 
 
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. //1.找的我们关心控件
  7. Button btn_contact = (Button) findViewById(R.id.btn_contact);
  8. Button btn_wx = (Button) findViewById(R.id.btn_wx);
  9. Button btn_discover = (Button) findViewById(R.id.btn_discover);
  10. Button btn_mine = (Button) findViewById(R.id.btn_mine);
  11. //2.给按钮设置点击事件
  12. btn_contact.setOnClickListener(this);
  13. btn_wx.setOnClickListener(this);
  14. btn_discover.setOnClickListener(this);
  15. btn_mine.setOnClickListener(this);
  16. }
  17. //具体判断一下点击按个按钮
  18. @Override
  19. public void onClick(View view) {
  20. //1.获取fragment管理者
  21. FragmentManager fragmentManager = getFragmentManager();
  22. //2.开启事务
  23. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  24. switch (view.getId()){
  25. case R.id.btn_wx: //点击消息按钮
  26. fragmentTransaction.replace(R.id.ll,new WxFragment());
  27. break;
  28. case R.id.btn_contact://点击了联系人按钮
  29. fragmentTransaction.replace(R.id.ll,new ContactFragment());
  30. break;
  31. case R.id.btn_discover://点击发现按钮
  32. fragmentTransaction.replace(R.id.ll,new DiscoverFragment());
  33. break;
  34. case R.id.btn_mine://点击我按钮
  35. fragmentTransaction.replace(R.id.ll,new MineFragment());
  36. break;
  37. }
  38. //3.提交事务
  39. fragmentTransaction.commit();
  40. }
  41. }

5.fragment生命周期

在实际开发中 只需要重写onCreateView 和 onDestroy方法就可够用了,在onCreateView方法里面初始化控件.在ondestroy方法里面做扫尾的工作.比如解绑服务 释放资源.

6.fragment之间通信 

   fragment之间公共桥梁---->Activity.

7.样式和主题

  主题和样式 定义的方式是一样的 都是在res下style.xml文件里面定义 
  样式和主题作用范围不一样,样式一般应用在控件上,主题一般应用在application或者是activity上面.

8.国际化

周杰伦  jay    jj:林俊杰   i18n---->国际化---->
 在res目录下创建各个缩略语言目录就可以了 


9.应用的反编译

   apktools 对apk资源(图片 布局  清单文件)进行反编译
   dex2jar   --->jd.exe 
   反编译步骤 --->直接使用android的逆向助手来反编译

10.属性动画

 
 
  1. public class MainActivity extends AppCompatActivity {
  2. private ImageView iv;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. //1.找到控件
  8. iv = (ImageView) findViewById(R.id.iv);
  9. //2.给iv设置点击事件
  10. iv.setOnClickListener(new View.OnClickListener() {
  11. @Override
  12. public void onClick(View view) {
  13. Toast.makeText(MainActivity.this, "你点不到我", Toast.LENGTH_SHORT).show();
  14. }
  15. });
  16. // iv.setAlpha();
  17. // iv.setRot
  18. // iv.setScaleY();
  19. // iv.setTranslationX();
  20. }
  21. //点击按钮 使用属性动画实现透明动画效果
  22. public void click1(View view) {
  23. //1.获取ObjectAnimator 实例 通过类提供的静态方法 参1:目标(谁执行动画) //参数2:属性名
  24. ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 1.0f, 0.8f, 0,7f, 0.4f, 0.0f,0.5f,0,9f,1.0f);
  25. //2.设置动画执行时长
  26. oa.setDuration(2000);
  27. //3.启动动画
  28. oa.start();
  29. }
  30. //点击按钮 使用属性动画实现旋转动画效果
  31. public void click2(View view) {
  32. //1.获取ObjectAnimator 实例 通过类提供的静态方法 参1:目标(谁执行动画) //参数2:属性名
  33. ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotation",0,90,180,360);
  34. //2.设置动画执行时长
  35. oa.setDuration(2000);
  36. //3.启动动画
  37. oa.start();
  38. }
  39. //点击按钮 使用属性动画实现缩放动画效果
  40. public void click3(View view) {
  41. //1.获取ObjectAnimator 实例 通过类提供的静态方法 参1:目标(谁执行动画) //参数2:属性名
  42. ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleX",1.0f,2.0f,3.0f,4.0f);
  43. //2.设置动画执行时长
  44. oa.setDuration(2000);
  45. //3.启动动画
  46. oa.start();
  47. }
  48. //点击按钮 使用属性动画实现位移动画效果
  49. public void click4(View view) {
  50. //1.获取ObjectAnimator 实例 通过类提供的静态方法 参1:目标(谁执行动画) //参数2:属性名
  51. ObjectAnimator oa = ObjectAnimator.ofFloat(iv,"translationX",0,10,30,100);
  52. //2.设置动画执行时长
  53. oa.setDuration(2000);
  54. //3.启动动画
  55. oa.start();
  56. }
  57. //让前面几个动画一起执行
  58. public void click5(View view) {
  59. //1,创建动画合集
  60. AnimatorSet set = new AnimatorSet();
  61. //2.创建不同动画效果
  62. ObjectAnimator oa1 = ObjectAnimator.ofFloat(iv, "alpha", 1.0f, 0.8f, 0,7f, 0.4f, 0.0f,0.5f,0,9f,1.0f);
  63. ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "rotation",0,90,180,360);
  64. ObjectAnimator oa3 = ObjectAnimator.ofFloat(iv, "scaleX",1.0f,2.0f,3.0f,4.0f);
  65. ObjectAnimator oa4 = ObjectAnimator.ofFloat(iv,"translationX",0,10,30,100);
  66. //3.让oa1 oa2 oa3 oa4 一起飞
  67. // set.playTogether(oa1,oa2,oa3,oa4);
  68. //3.1 让oa1 oa2 oa3 oa4 按顺序执行
  69. set.playSequentially(oa1,oa2,oa3,oa4);
  70. //4.谁来执行动画
  71. set.setTarget(iv);
  72. //5.设置动画执行时长
  73. set.setDuration(2000);
  74. //6.启动动画
  75. set.start();
  76. }
  77. }
属性动画 会改变控件真实的坐标.

11.通知

  1.给用户友好提示的方式: 1Toast  2 对话框 3,通知 
   2.我们学过的manager: DriverManager,SmsManager,WindowManager, TelephonyManager ,FragmentManager, NotificationManager
                                          PackageManager  ActivityManager 
  
 
 
  1. public class MainActivity extends AppCompatActivity {
  2. private NotificationManager nm;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. }
  8. //点击按钮 弹出通知
  9. public void click1(View view) {
  10. //1.获取通知的管理者
  11. nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  12. //1.0创建意图
  13. Intent intent = new Intent();
  14. //1.1给小芳打个电话问问清楚
  15. intent.setAction(Intent.ACTION_CALL);
  16. intent.setData(Uri.parse("tel:"+110));
  17. //1.2 构造pendingIntent
  18. PendingIntent pi = PendingIntent.getActivities(this, 0, new Intent[]{intent}, PendingIntent.FLAG_ONE_SHOT);
  19. //2.构造notification 链式调用:每调用方法返回值都是一样的
  20. Notification noti = new Notification.Builder(this)
  21. .setContentTitle("小芳") //设置通知标题
  22. .setContentText("老地方件") //设置标题描述
  23. .setSmallIcon(R.mipmap.ic_launcher) //设置小图标
  24. .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))//设置大图标
  25. .setContentIntent(pi) //点击通知做的事情
  26. .setDefaults(Notification.DEFAULT_ALL)
  27. .build();
  28. //2.弹出通知
  29. nm.notify(10,noti);
  30. }
  31. //点击按钮 关闭通知
  32. public void click2(View view) {
  33. nm.cancel(10);
  34. }

   







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值