Fragment添加到Activity中的方式
xml中使用fragment标签来添加
Java中FragmentManager来操作
Activity中管理内部的Fragment的Manager获取方式
Fragment中:
getFragmentManager()
FragmentActivity中:
getSupportFragmentManager()
Fragment中管理子Fragment:
getChildFragmentManager()
Fragment生命周期
xml添加的方式,生命周期情况
Activity Fragment
onCreate onAttach
onCreate
onCreateView
onActivityCreate
onStart onStart
onResume onResume
onPause onPause
onStop onStop
onDestroy onDestroyView
onDestroy
onDettach
动态管理:
replace:等效于add新的remove旧的
旧的:onPause、onStop、onDestroyView
新的:onAttach、onCreate、onCreateView、onActivityCreate、onStart、onResume
旧的:onDestroy、onDettach
detach、attach:旧的会销毁视图,新的创建视图
show、hide:不影响生命周期
ViewPager+Fragment
可以使用FragmentPagerAdapter(attach和dettach),FragmentStatePagerAdapter(add和remove)来配置Fragment在ViewPager中的显示
class MyAdapter extends FragmentPagerAdapter {
private Fragment[] fs;
public MyAdapter(FragmentManager fm) {
super(fm);
fs = new Fragment[] { RightFragment.newInstance("碎片0", 0),
RightFragment.newInstance("碎片1", 1),
RightFragment.newInstance("碎片2", 2),
RightFragment.newInstance("碎片3", 3),
RightFragment.newInstance("碎片4", 4) };
}
@Override
public Fragment getItem(int position) {
return fs[position];
}
@Override
public int getCount() {
return fs.length;
}
}
...
ViewPager mPager = (ViewPager) findViewById(R.id.m_pager);
MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
mPager.setAdapter(adapter);
Fragment跳转到Activity
s Intent it = new Intent(getActivity(), ViewPagerFragmentActivity.class); startActivity(it); 第二种
Intent it2 = new Intent(getActivity(), Activity02.class);
startActivityForResult(it2, 101);
重写onActivityResult方法接收结果
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("m_tag","resultCode:"+resultCode);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
String str = data.getStringExtra("result");
tv.setText(str);
} else {
tv.setText("用户取消");
}
}
}
注意Fragment依赖的Activity中的onActivityForResult的super.onActivityResult(requestCode, resultCode, data)方法需要保留,否则信息传不到Fragment中
Eclipse中发布正式的apk
项目上右单击–>Android Tools–>Expore Sign Application Package–>选择项目,next–>第一次可以选择Create..–>选择签名文件的位置和输入签名文件密
码–>添加签名信息
Alias: 签名信息的别名
Password: 签名信息的密码
Confirm: 确认密码
Validity: 有效年限(最多1000年)
First and Last Name: 开发者的姓氏或者名称
Organizational Unit: 组织单位部门
Organizational: 组织单位名称
City Or Province: 城市或者省
State or Province: 省或者区
Country Code: 国家代号(86)
输入完信息之后,next–>选择apk保存位置,finish即可
第二次之后的打包直接选择已有的签名文件,输入文件密码,next–>选择签名信息(别名),输入签名信息密码,next–>选择apk保存位置,finish即可
Studio的项目结构
Studio中的Project相当于Eclipse的WorkSpace,Studio中的module相当于Eclipse中的project
ConstraintLayout约束布局
使用(在module的build.gradle添加依赖):
implementation ‘com.android.support.constraint:constraint-layout:1.1.0’
认识基本属性,取值如果parent表示参考的是父容器
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
以app:layoutconstraintBottomtoBottomOf=”parent”为例,constraintBottom表示约束视图的底部(在该视图底部加一条橡皮筋,拉),toBottomOf约束到哪个
视图上(拉到哪里),parent表示父容器
app:layout_constraintLeft_toRightOf="@id/tv" 将试图的左边拉到id为tv的视图的右边
app:layout_constraintBaseline_toBaselineOf="@id/tv" 将视图的参考线跟id为tv的视图的参考线对齐
layout_constraintBottom_toTopOf
layout_constraintRight_toLeftOf
layout_constraintTop_toBottomOf
宽高比
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff666666"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintDimensionRatio="H,16:9"/>
app:layout_constraintDimensionRatio="16:9"可以设置宽高比冒号前面是宽,后面是高,还有另外的写法
app:layout_constraintDimensionRatio="H,16:9" 表示宽固定(铺满父容器)高按照比例计算
app:layout_constraintDimensionRatio="W,16:9" 表示高固定(铺满父容器)宽按比例计算
偏移
bias属性可以控制水平或者垂直的权重(拉的力度)
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABC"
android:background="#ff666666"
android:textColor="#ffffff"
app:layout_constraintVertical_bias="0.7" 垂直方向设置距离顶部占70%距离
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.3"/> 水平方向设置距离左边占30%距离
链式布局
可以设置视图之间前后参考,实现均匀排列的效果,如下,从左到右排列
<TextView
android:id="@+id/tab_0"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#ff0000"
android:gravity="center"
android:text="选项卡1"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/tab_1"/>
<TextView
android:id="@id/tab_1"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#00ff00"
android:gravity="center"
android:text="选项卡1"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/tab_0"
app:layout_constraintRight_toLeftOf="@+id/tab_2"/>
<TextView
android:id="@id/tab_2"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#ff00ff"
android:gravity="center"
android:text="选项卡1"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/tab_1"
app:layout_constraintRight_toRightOf="parent"/>
其中水平排列,如果宽度是0则会1:1拉伸(均分),同时可以设置
app:layout_constraintHorizontal_weight="1"
设置权重来调整宽度(或高)比例
如果水平编排时宽度固定(垂直编排高度固定),可以使用
app:layout_constraintHorizontal_chainStyle
设置样式
packed:视图都挤在一起放在中间部分
___口口口___
spread:间距一样均匀排列
_口_口_口_
spread_inside:链头和链尾被拉到参考的目标对应的位置,剩下的均分
口__口__口
参考线
可以在布局中作为参考元素,不会显示到界面上
<android.support.constraint.Guideline
android:id="@+id/guide_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"/>
android:orientation设置参考线的方向 水平或者垂直
app:layout_constraintGuide_percent参考线的百分比位置了,水平参考线表示距离顶部的百分比,垂直表示距离左侧的百分比
app:layout_constraintGuide_start 水平参考线表示距离顶部的大小,垂直表示距离左侧的大小
app:layout_constraintGuide_end 水平参考线表示距离底部的大小,垂直表示距离右侧的大小