鱼C论坛

 找回密码
 立即注册
查看: 2776|回复: 0

[学习笔记] android programing 4.4

[复制链接]
发表于 2017-10-5 08:42:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
碎片的生命周期跟activity的差不多,这里就不多说了,以下是我网上找的流程图
0_1326006184miBx.gif


下面主要说的是如何适配不同尺寸的手机屏幕。首先在res目录下新建个layout-large目录,把layout_activity.xml文件复制一份到这个目录里,然后把原来layout目录下的layout_activity.xml修改为:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="horizontal" android:layout_width="match_parent"
  4.     android:layout_height="match_parent">

  5.     <fragment
  6.         android:layout_width="0dp"
  7.         android:layout_height="match_parent"
  8.         android:layout_weight="1"
  9.         android:id="@+id/left_fragment"
  10.         android:name="com.example.xinwei.fragmentactivity.LeftFragment" />
  11.    
  12. </LinearLayout>
复制代码

然后把mainactivity.java文件的两句调用replaceFragement()函数注释掉
  1. package com.example.xinwei.fragmentactivity;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.support.v4.app.Fragment;
  5. import android.support.v4.app.FragmentManager;
  6. import android.support.v4.app.FragmentTransaction;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.TextView;

  12. public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.         Button button = (Button)findViewById(R.id.button);
  18.         button.setOnClickListener(this);
  19.         Button button1 = (Button)findViewById(R.id.button1);
  20.         button1.setOnClickListener(this);
  21.         replaceFragment(new RightFragment());
  22.     }

  23.     private void replaceFragment(Fragment fragment) {
  24.         FragmentManager fragmentManager=getSupportFragmentManager();
  25.         FragmentTransaction transition=fragmentManager.beginTransaction();
  26.         transition.addToBackStack(null);//按back键可以返回
  27. //        transition.replace(R.id.right_layout,fragment);
  28.         transition.commit();
  29.     }

  30.     @Override
  31.     public void onClick(View view) {
  32.         switch(view.getId()){
  33.             case R.id.button:
  34. //                replaceFragment(new AnotherRightFragment());
  35.                 break;
  36.             case R.id.button1:
  37.                 TextView textView=(TextView)findViewById(R.id.text_view);
  38.                 if(textView!=null){
  39.                     textView.setText("你好");
  40.                 }

  41.                 break;
  42.         }
  43.     }



  44. }
复制代码

把此项目部署到不同尺寸的浏览器,就会有一个适配不同屏幕的效果,如图:
aaaaaaaaaaaa.png
最小宽度限定也就是把刚才的layout_large文件夹修改成layout-sw600dp就行了,这里就不演示了

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 19:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表