CardView的基本使用、DrawerLayout 滑动菜单、Fragment

1 CardView

1.CardView的基本使用

CardView是用于实现卡片式布局效果的重要控件,实际上也是一个frameLayout,只是额外提供了圆角和 阴影,看上去有立体效果。

常用API:

基本使用方法 :

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardElevation="5dp"
app:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:scaleType="centerCrop"
app:srcCompat="@mipmap/a1" />
<TextView
android:id="@+id/tv_title"
android:text="AI 改变千行万业,开发者如何投身 AI 语音新“声”态"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="5dp"
android:textSize="16sp" />
</LinearLayout>

2 DrawerLayout 滑动菜单

DrawerLayout包含两个界面,一个主界面和一个隐藏界面。隐藏界面可以通过点击按钮或者滑动屏幕边缘显示出来,一般隐藏界面用来做菜单使用。

DrawerLayout是一个布局,和普通布局使用起来没有多大差别,先在布局文件中添加一个

DrawerLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
 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=".MainActivity3">
 <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="主界面" />
<TextView
        android:id="@+id/textView2"
        android:layout_gravity="start"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="滑出页面"
        android:background="#fff"/>
</androidx.drawerlayout.widget.DrawerLayout>

DrawerLayout包含两个直接子控件(或布局)

        第一个将显示在主界面,第二个将显示在子界面。

        第二个控件(或布局)中必须添加android:layout_gravity 属性,

 android:layout_gravity 有三个值:left,right,start.分别表示隐藏菜单从左边,右边,根据系统语言习惯选择从左边还是右边滑出。

到这里滑动菜单就已经设置完成了,但是为了方便用户体验,我们一般会设置一个按钮用来提醒用户隐藏界面的存在

3 Fragment

3.1 Fragment的概念

3.1.1 Fragment的简介

1. Fragment是依赖于Activity的,不能独立存在。

2. 一个Activity里可以有多个Fragment。

3. 一个Fragment可以被多个Activity重用。

4. Fragment有自己的生命周期,并能接收输入事件。

5. 可以在Activity运行时动态地添加或删除Fragment。

 3.1.2 Fragment生命周期

 3.2 Fragment的静态加载

静态加载--以标签的形式添加到Activity的布局当中。

 3.3 Fragment的动态使用

Fragment真正的强大之处在于可以动态地添加到Activity当中,因此这也是你必须要掌握的东西。当你学会了在程序运行时向Activity添加Fragment,程序的界面就可以定制的更加多样化。

 1. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity5">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="第一个碎片" />
<Button
android:id="@+id/btn_fragment2"
2. 创建Fragment1
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="第二个碎片" />
</LinearLayout>
<FrameLayout
android:id="@+id/lay_fl"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

2. 创建Fragment1

public class Fragment1 extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public Fragment1() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Fragment1.
*/
// TODO: Rename and change types and number of parameters
public static Fragment1 newInstance(String param1, String param2) {
Fragment1 fragment = new Fragment1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
3. 创建Fragment2
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_1, container, false);
}
}
<?xml version="1.0" encoding="utf-8"?>
<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="#DC8888"
tools:context=".fragment.Fragment1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="24sp"
android:text="第一个碎片" />
</FrameLayout>

3. 创建Fragment2

public class Fragment2 extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public Fragment2() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
4. 在活动中,点击按钮后切换Fragment
* @return A new instance of fragment Fragment2.
*/
// TODO: Rename and change types and number of parameters
public static Fragment2 newInstance(String param1, String param2) {
Fragment2 fragment = new Fragment2();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_2, container, false);
}
}

4. 在活动中,点击按钮后切换Fragmen

public class MainActivity5 extends AppCompatActivity {
private Button btn_fragment1, btn_fragment2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
btn_fragment1 = findViewById(R.id.btn_fragment1);
btn_fragment2 = findViewById(R.id.btn_fragment2);
btn_fragment1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment1 fragment1 = new Fragment1();
getSupportFragmentManager().beginTransaction().replace(R.id.lay_fl,
fragment1).show(fragment1).commitAllowingStateLoss();
}
});
btn_fragment2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment2 fragment2 = new Fragment2();
getSupportFragmentManager().beginTransaction().replace(R.id.lay_fl,
fragment2).show(fragment2).commitAllowingStateLoss();
}
});
}

猜你喜欢

转载自blog.csdn.net/shuo277/article/details/126184916
今日推荐