Basic use of CardView, DrawerLayout sliding menu, Fragment

1 CardView

1. Basic use of CardView

CardView is an important control for realizing the effect of card layout. It is actually a frameLayout, but additionally provides rounded corners and shadows, which looks three-dimensional.

Commonly used APIs:

Basic usage method:

<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 sliding menu

DrawerLayout contains two interfaces, a main interface and a hidden interface. The hidden interface can be displayed by clicking the button or sliding the edge of the screen. Generally, the hidden interface is used as a menu.

DrawerLayout is a layout, which is not much different from ordinary layouts. First add a layout file

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 contains two direct child controls (or layouts)

        The first one will be displayed on the main interface, and the second one will be displayed on the sub-interface.

        The android:layout_gravity attribute must be added to the second control (or layout),

 android:layout_gravity has three values: left, right, start. Respectively indicate that the hidden menu slides out from the left or the right according to the system language habits.

The sliding menu has been set up here, but in order to facilitate the user experience, we generally set a button to remind the user of the existence of the hidden interface

3 Fragment

3.1 The concept of Fragment

3.1.1 Introduction to Fragments

1. Fragment is dependent on Activity and cannot exist independently.

2. There can be multiple Fragments in an Activity.

3. A Fragment can be reused by multiple Activities.

4. Fragment has its own life cycle and can receive input events.

5. Fragments can be added or removed dynamically when the Activity is running.

 3.1.2 Fragment life cycle

 3.2 Static loading of Fragment

Static loading--add to the layout of the Activity in the form of a label.

 3.3 Dynamic use of Fragment

The real power of Fragment is that it can be dynamically added to the Activity, so this is something you must master. When you learn to add Fragment to Activity when the program is running, the interface of the program can be customized more diversified.

 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. Create 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. Create 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. In the activity, toggle the Fragmen after clicking the button

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();
}
});
}

Guess you like

Origin blog.csdn.net/shuo277/article/details/126184916