目录
2. 在需要加载Fragment的Activity对应的布局文件中添加fragment的标签
CardView
什么是CardView;
CardView是用于实现卡片式布局效果的重要控件,实际上也是一个frameLayout,只是额外提供了圆角和 阴影,看上去有立体效果。
CardView是一个布局,其实是一个FrameLayout,只不过多了圆角和阴影
FrameLayout是一个帧布局,在后面的就是优先级高的就会往上一帧压
CardView使用方法
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
app:cardElevation="5dp"
android:layout_margin="5dp"
app:cardCornerRadius="5dp"
android:layout_height="200dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_img"
android:layout_width="150dp"
android:layout_height="200dp"
app:srcCompat="@mipmap/baozi" />
<TextView
android:id="@+id/tv_title"
android:layout_marginStart="15dp"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</androidx.cardview.widget.CardView>
常用属性
方法 | 说明 |
cardBackgroundColor | 设置圆角边大小 |
cardElevation | 阴影大小- 设置为0代表没有阴影 |
cardMaxElevation | 最大的阴影大小 |
cardPreventCornerOverlap | 在v20和之前的版本中添加内边距,这个属性是为了防止卡片内容 和边角的重叠 |
cardUseCompatPadding | 设置内边距,v21+的版本和之前的版本仍旧具有一样的计算方式 |
contentPadding | 内边距 |
contentPaddingBottom | 底部边距 |
contentPaddingLeft | 左侧边距 |
contentPaddingRight | 右侧边距 |
contentPaddingTop | 顶部边距 |
Fragment
什么是Fragment?
Fragment被译为碎片
1. Fragment是依赖于Activity的,不能独立存在。
2. 一个Activity里可以有多个Fragment。
3. 一个Fragment可以被多个Activity重用。
4. Fragment有自己的生命周期,并能接收输入事件。
5. 可以在Activity运行时动态地添加或删除Fragment。
Fragment生命周期
常见生命周期
1. Activity加载Fragment的时候,依次调用:onAttach() -> onCreate() -> onCreateView() -> onActivityCreated() -> onStart() ->onResume()
2. 当做出一个悬浮的对话框风格的Activity,或者其他,就是让Fragment所在的Activity可见,但不获 得焦点:onPause()
3. 当对话框关闭,Activity又获得了焦点: onResume()
4. 当替换Fragment,并调用addToBackStack()将它添加到Back栈中:onPause() -> onStop() -> onDestoryView() 。注意,此时的Fragment还没有被销毁哦。
5. 当按下键盘的回退键,Fragment会再次显示出来:onCreateView() -> onActivityCreated() -> onStart() -> onResume()
6. 如果替换后,在事务commit之前没有调用addToBackStack()方法将Fragment添加到back栈中, 或者退出了Activity的话,那么Fragment将会被完全结束,Fragment会进入销毁状态: onPause() -> onStop() -> onDestoryView() -> onDestory() -> onDetach()
Fragment的静态加载
创建Fragment
1. 定义Fragment的布局
<FrameLayout 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="@color/purple_700"
tools:context=".fragment.Fragment1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="第一" />
</FrameLayout>
2. 在需要加载Fragment的Activity对应的布局文件中添加fragment的标签
!!!注意name属性是全限定 类名,就是要包含Fragment的包名。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.wzk.fragmentdemo.fragment.EndFragment"
android:layout_gravity="bottom"
android:id="@+id/fg_nn"/>
</LinearLayout>
Fragment的动态使用
创建主布局文件
!!!注意主布局是FrameLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<Button
android:id="@+id/btn_frament1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_frament2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lay_ly"/>
</LinearLayout>
创建Frament
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@color/black" android:layout_height="match_parent"
tools:context=".fragment.Fragment2">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="第二" />
</FrameLayout>
在主活动中使用Frament
Button btn_frament1;
Button btn_frament2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btn_frament1 = findViewById(R.id.btn_frament1);
btn_frament2 = findViewById(R.id.btn_frament2);
btn_frament1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment1 fragment1=new Fragment1();
getSupportFragmentManager().beginTransaction().replace(R.id.lay_ly,fragment1).commit();
}
});
btn_frament2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment2 fragment2=new Fragment2();
getSupportFragmentManager().beginTransaction().replace(R.id.lay_ly,fragment2).commit();
}
});
}