1.fragment介绍
fragment总是被嵌入到Activity里 fragment要添加到ViewGroup里
fragment是在android3.0的时候被引入的.
代码实现
1.在布局声明
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="itheima.day12.Test1Fragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="itheima.day12.Test2Fragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
2.声明fragment 需要重写onCreateView方法
//创建了一个fragment 代表一个页面
public class Test1Fragment extends Fragment {
//通过下面这个方法加载布局
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//加载布局 通过打气筒加载
return inflater.inflate(R.layout.fragment_test1, null);
}
}
2.动态添加fragment
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.获取手机宽高
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
//2.获取一个fragment的管理者
FragmentManager fragmentManager = getFragmentManager();
//2.1 开启fragment事务
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//3.判断手机状态是横屏还是竖屏
if (height > width){
//说明是竖屏 加载一个页面 参数1:android.代表系统定义好的一些id 理解成是当前手机窗口
fragmentTransaction.replace(android.R.id.content,new Test1Fragment());
}else{
//说明是横屏 加载另外一个页面
fragmentTransaction.replace(android.R.id.content,new Test2Fragment());
}
//4.最后一步 记得提交事务
fragmentTransaction.commit();
}
}
3.通过v4包中的fragment支持低版本.
兼容低版本就是使用V4包中的fragment .
注意地方:在获取fragm管理者的方式不一样了
FragmentManager supportFragmentManager = getSupportFragmentManager();
4.通过fragment模拟一个微信主页面
1.在布局中声明UI
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="itheima.wechat.MainActivity">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/btn_wx"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="消息" />
<Button
android:id="@+id/btn_contact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="联系人" />
<Button
android:id="@+id/btn_discover"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="发现" />
<Button
android:id="@+id/btn_mine"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="我" />
</LinearLayout>
</RelativeLayout>
2.声明四个fragment,每个fragmen分别代表一个页面
3.根据UI写对应逻辑
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.找的我们关心控件
Button btn_contact = (Button) findViewById(R.id.btn_contact);
Button btn_wx = (Button) findViewById(R.id.btn_wx);
Button btn_discover = (Button) findViewById(R.id.btn_discover);
Button btn_mine = (Button) findViewById(R.id.btn_mine);
//2.给按钮设置点击事件
btn_contact.setOnClickListener(this);
btn_wx.setOnClickListener(this);
btn_discover.setOnClickListener(this);
btn_mine.setOnClickListener(this);
}
//具体判断一下点击按个按钮
@Override
public void onClick(View view) {
//1.获取fragment管理者
FragmentManager fragmentManager = getFragmentManager();
//2.开启事务
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (view.getId()){
case R.id.btn_wx: //点击消息按钮
fragmentTransaction.replace(R.id.ll,new WxFragment());
break;
case R.id.btn_contact://点击了联系人按钮
fragmentTransaction.replace(R.id.ll,new ContactFragment());
break;
case R.id.btn_discover://点击发现按钮
fragmentTransaction.replace(R.id.ll,new DiscoverFragment());
break;
case R.id.btn_mine://点击我按钮
fragmentTransaction.replace(R.id.ll,new MineFragment());
break;
}
//3.提交事务
fragmentTransaction.commit();
}
}
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.属性动画
public class MainActivity extends AppCompatActivity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.找到控件
iv = (ImageView) findViewById(R.id.iv);
//2.给iv设置点击事件
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "你点不到我", Toast.LENGTH_SHORT).show();
}
});
// iv.setAlpha();
// iv.setRot
// iv.setScaleY();
// iv.setTranslationX();
}
//点击按钮 使用属性动画实现透明动画效果
public void click1(View view) {
//1.获取ObjectAnimator 实例 通过类提供的静态方法 参1:目标(谁执行动画) //参数2:属性名
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 1.0f, 0.8f, 0,7f, 0.4f, 0.0f,0.5f,0,9f,1.0f);
//2.设置动画执行时长
oa.setDuration(2000);
//3.启动动画
oa.start();
}
//点击按钮 使用属性动画实现旋转动画效果
public void click2(View view) {
//1.获取ObjectAnimator 实例 通过类提供的静态方法 参1:目标(谁执行动画) //参数2:属性名
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotation",0,90,180,360);
//2.设置动画执行时长
oa.setDuration(2000);
//3.启动动画
oa.start();
}
//点击按钮 使用属性动画实现缩放动画效果
public void click3(View view) {
//1.获取ObjectAnimator 实例 通过类提供的静态方法 参1:目标(谁执行动画) //参数2:属性名
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleX",1.0f,2.0f,3.0f,4.0f);
//2.设置动画执行时长
oa.setDuration(2000);
//3.启动动画
oa.start();
}
//点击按钮 使用属性动画实现位移动画效果
public void click4(View view) {
//1.获取ObjectAnimator 实例 通过类提供的静态方法 参1:目标(谁执行动画) //参数2:属性名
ObjectAnimator oa = ObjectAnimator.ofFloat(iv,"translationX",0,10,30,100);
//2.设置动画执行时长
oa.setDuration(2000);
//3.启动动画
oa.start();
}
//让前面几个动画一起执行
public void click5(View view) {
//1,创建动画合集
AnimatorSet set = new AnimatorSet();
//2.创建不同动画效果
ObjectAnimator oa1 = ObjectAnimator.ofFloat(iv, "alpha", 1.0f, 0.8f, 0,7f, 0.4f, 0.0f,0.5f,0,9f,1.0f);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "rotation",0,90,180,360);
ObjectAnimator oa3 = ObjectAnimator.ofFloat(iv, "scaleX",1.0f,2.0f,3.0f,4.0f);
ObjectAnimator oa4 = ObjectAnimator.ofFloat(iv,"translationX",0,10,30,100);
//3.让oa1 oa2 oa3 oa4 一起飞
// set.playTogether(oa1,oa2,oa3,oa4);
//3.1 让oa1 oa2 oa3 oa4 按顺序执行
set.playSequentially(oa1,oa2,oa3,oa4);
//4.谁来执行动画
set.setTarget(iv);
//5.设置动画执行时长
set.setDuration(2000);
//6.启动动画
set.start();
}
}
属性动画 会改变控件真实的坐标.
11.通知
1.给用户友好提示的方式: 1Toast 2 对话框 3,通知
2.我们学过的manager: DriverManager,SmsManager,WindowManager,
TelephonyManager
,FragmentManager,
NotificationManager
PackageManager ActivityManager
public class MainActivity extends AppCompatActivity {
private NotificationManager nm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//点击按钮 弹出通知
public void click1(View view) {
//1.获取通知的管理者
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//1.0创建意图
Intent intent = new Intent();
//1.1给小芳打个电话问问清楚
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+110));
//1.2 构造pendingIntent
PendingIntent pi = PendingIntent.getActivities(this, 0, new Intent[]{intent}, PendingIntent.FLAG_ONE_SHOT);
//2.构造notification 链式调用:每调用方法返回值都是一样的
Notification noti = new Notification.Builder(this)
.setContentTitle("小芳") //设置通知标题
.setContentText("老地方件") //设置标题描述
.setSmallIcon(R.mipmap.ic_launcher) //设置小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))//设置大图标
.setContentIntent(pi) //点击通知做的事情
.setDefaults(Notification.DEFAULT_ALL)
.build();
//2.弹出通知
nm.notify(10,noti);
}
//点击按钮 关闭通知
public void click2(View view) {
nm.cancel(10);
}