Catalog
3. Realization of Basic Framework
3. Overall Framework activity_main,xml
4. Click Switch Effect Realization
1. Design of fragment interface
2. Click events corresponding to fragments
5. Implementation of corresponding functions in MainActivity
app effect display:
I. Major Tasks
Implement APP Portal Interface Based on Course Practice frame Designed to contain at least 4 tab pages to enable click-to-click switching between tab pages;
2. Control Use
Using layouts and segmentation ( fragment ), click on the control to listen.
3. Realization of Basic Framework
The basic framework can be divided into three parts: the top title, the middle FrameLayout, and the bottom four click-and-switch actions
1. top design
At the top, you need a TextView with a horizontal LinearLayout layout at the outermost level
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="55dp" android:background="@color/black"> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="55dp" android:layout_weight="1" android:gravity="center" android:text="WeChat APP" android:textColor="@color/white" android:textSize="30sp" /> </LinearLayout>
2. Bottom design
The bottom four click-and-switch actions each require an ImageView and a TextView, and two controls are contained within the same LinearLayout, and the last four LinearLayouts use a horizontal LinearLayout layout for their outer layouts.
<?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:layout_height="100dp" android:background="@color/black" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/imageView2" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/p15"/> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="WeChat" android:textColor="@color/white" android:textSize="24sp" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/imageView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/p14" /> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Mail list" android:textColor="@color/white" android:textSize="24sp" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/imageView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/p16" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="find" android:textColor="@color/white" android:textSize="24sp" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/p17" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="I" android:textColor="@color/white" android:textSize="24sp" /> </LinearLayout> </LinearLayout>
3. Overall Framework 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:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <include layout="@layout/top" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@+id/id_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </FrameLayout> <include layout="@layout/bottom" android:gravity="bottom" /> </LinearLayout>
4. Click Switch Effect Realization
To achieve the effect of click switching, you need to set up a FragmentManager to control the four different tabs, so that they can be overlaid and displayed with one tab and the other three hidden when different click actions occur.
1. Design of fragment interface
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> tools:context=".Fragment_wechat"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="This is the WeChat interface" android:textSize="35sp" /> </androidx.constraintlayout.widget.ConstraintLayout>
The other three fragment s are written similarly;
2. Click events corresponding to fragments
package com.example.myapplication; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment_config extends Fragment { public Fragment_config() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_config, container, false); } }
The other three click events are written similarly;
5. Implementation of corresponding functions in MainActivity
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Fragment Fragment_config=new Fragment_config(); private Fragment Fragment_contact=new Fragment_contact(); private Fragment Fragment_wechat=new Fragment_wechat(); private Fragment Fragment_friend=new Fragment_friend(); private FragmentManager fragmentManager; private LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); linearLayout1=findViewById(R.id.linearLayout1); linearLayout2=findViewById(R.id.linearLayout2); linearLayout3=findViewById(R.id.linearLayout3); linearLayout4=findViewById(R.id.linearLayout4); linearLayout1.setOnClickListener(this); linearLayout2.setOnClickListener(this); linearLayout3.setOnClickListener(this); linearLayout4.setOnClickListener(this); initFragment(); } private void initFragment(){ fragmentManager=getFragmentManager(); FragmentTransaction transaction=fragmentManager.beginTransaction(); transaction.add(R.id.id_content,Fragment_wechat); transaction.add(R.id.id_content,Fragment_contact); transaction.add(R.id.id_content,Fragment_config); transaction.add(R.id.id_content,Fragment_friend); hideFragment(transaction); transaction.commit(); } private void hideFragment(FragmentTransaction transaction){ transaction.hide(Fragment_wechat); transaction.hide(Fragment_contact); transaction.hide(Fragment_config); transaction.hide(Fragment_friend); } private void background(View v) { switch (v.getId()) { case R.id.linearLayout1: linearLayout1.setBackgroundColor(Color.parseColor("#426F42")); break; case R.id.linearLayout2: linearLayout2.setBackgroundColor(Color.parseColor("#426F42")); break; case R.id.linearLayout3: linearLayout3.setBackgroundColor(Color.parseColor("#426F42")); break; case R.id.linearLayout4: linearLayout4.setBackgroundColor(Color.parseColor("#426F42")); break; default: break; } } private void backgroundreturn(View v) { switch (v.getId()) { case R.id.linearLayout1: linearLayout1.setBackgroundColor(Color.parseColor("#000000")); break; case R.id.linearLayout2: linearLayout2.setBackgroundColor(Color.parseColor("#000000")); break; case R.id.linearLayout3: linearLayout3.setBackgroundColor(Color.parseColor("#000000")); break; case R.id.linearLayout4: linearLayout4.setBackgroundColor(Color.parseColor("#000000")); break; default: break; } } private void showfragmnet(int i) { FragmentTransaction transaction=fragmentManager.beginTransaction(); hideFragment(transaction); switch (i){ case 0: transaction.show(Fragment_wechat); background(linearLayout1); backgroundreturn(linearLayout3); backgroundreturn(linearLayout2); backgroundreturn(linearLayout4); break; case 1: transaction.show(Fragment_friend); background(linearLayout2); backgroundreturn(linearLayout4); backgroundreturn(linearLayout1); backgroundreturn(linearLayout3); break; case 2: transaction.show(Fragment_contact); background(linearLayout3); backgroundreturn(linearLayout4); backgroundreturn(linearLayout2); backgroundreturn(linearLayout1); break; case 3: transaction.show(Fragment_config); background(linearLayout4); backgroundreturn(linearLayout1); backgroundreturn(linearLayout2); backgroundreturn(linearLayout3); break; default: break; } transaction.commit(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.linearLayout1: showfragmnet(0); break; case R.id.linearLayout2: showfragmnet(1); break; case R.id.linearLayout3: showfragmnet(2); break; case R.id.linearLayout4: showfragmnet(3); break; default: break; } } }
6. Summary
In this experiment, I am familiar with the use of several basic components, such as LinearLayout, ImageView, etc. By importing top and bottom, the top and bottom layouts are implemented, and fragment s are used to show and hide the corresponding interfaces.