實驗環境:
在Android Studio中進行有關代碼的撰寫和界面效果展示
圖示來源于阿里矢量圖示庫
界面分析:

點擊底端相應區域,中間內容會進行相應切換(將對應的Fragment顯示,其余的Fragment隱藏),下面展示點擊聯系人后,中間內容的變化,

同時點擊選單后,該選單的圖示和文字都將切換為綠色,

界面布局的代碼實作:
- 頂部top.xml
- 底端bottom.xml
- 中間內容四個分段fragment.xml
- 表單總布局的activity_main.xml
- 四個分段fragment.xml對應的.java檔案
- 主函式檔案MainActivity.java
頂部top.xml
(布局可以通過設定android:background屬性改變背景顏色,文本可以通過設定android:textColor屬性改變字體顏色,)
<?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="50dp"
android:background="@color/black">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text="我的微信"
android:textColor="@color/white"
android:textSize="24sp" />
</LinearLayout>
底端bottom.xml
(為節省篇幅此處只給出了第一個垂直式的LinearLayout-聊天選單)
android:gravity屬性可以定義布局內控制元件的對齊方式,居中、水平居中和垂直居中
android:orientation屬性可以定義布局內控制元件的排列方式,一般有水平式和垂直式
<?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"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/colorViewNormal"
android:gravity="center">
<LinearLayout
android:id="@+id/chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/chat" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/black"
android:text="聊天"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
中間內容區四個fragment.xml
(同樣只給出了一個分段,其余三個分段類似)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".Fragment_chat">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center"
android:text="這是“我的微信”的聊天界面!"
android:textColor="@color/black"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
(包含top、middle、bottom三個部分id,用 include 陳述句呼叫頂部和底部導航欄兩個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"></include>
<FrameLayout
android:id="@+id/frame_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
<include layout="@layout/bottom"></include>
</LinearLayout>
fragment_chat.java
(同樣只給出點擊聊天選單后顯示的分段,其余三個類似)
package com.example.mywork;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class Fragment_chat extends Fragment {
public Fragment_chat() {
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_chat,container,false);
}
}
MainActivity.java
(各組件的初始化操作及點擊各選單的轉換操作函式)
package com.example.mywork;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//Fragment
private Fragment fragment_first=new Fragment_chat();
private Fragment fragment_second=new Fragment_contacts();
private Fragment fragment_third=new Fragment_circle_friend();
private Fragment fragment_fourth=new Fragment_settings();
//底端選單欄LinearLayout
private LinearLayout linear_first;
private LinearLayout linear_second;
private LinearLayout linear_third;
private LinearLayout linear_fourth;
//底端選單欄中的Imageview
private ImageView imageView_first;
private ImageView imageView_second;
private ImageView imageView_third;
private ImageView imageView_fourth;
//底端選單欄中的TextView
private TextView textView_first;
private TextView textView_second;
private TextView textView_third;
private TextView textView_fourth;
//FragmentManager
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE );
setContentView(R.layout.activity_main);
initView();
initFragment();
initEvent();
selectFragment(0);
//將第一個圖示設為選中狀態
imageView_first.setImageResource(R.drawable.chat_green);
textView_first.setTextColor(getResources().getColor(R.color.colorViewPress));
}
@Override
public void onClick(View view) {
//每次點擊之后,將所有的ImageView和TextView設定為未選中
restartButton();
switch(view.getId())
{
case R.id.chat:
//選擇所點擊的選單對應的圖層片段
selectFragment(0);
//將該選單的點擊狀態置為點擊態
imageView_first.setImageResource(R.drawable.chat_green);
textView_first.setTextColor(getResources().getColor(R.color.colorViewPress));
break;
case R.id.contacts:
selectFragment(1);
imageView_second.setImageResource(R.drawable.contacts_green);
textView_second.setTextColor(getResources().getColor(R.color.colorViewPress));
break;
case R.id.circle_friend:
selectFragment(2);
imageView_third.setImageResource(R.drawable.circle_people_green);
textView_third.setTextColor(getResources().getColor(R.color.colorViewPress));
break;
case R.id.settings:
selectFragment(3);
imageView_fourth.setImageResource(R.drawable.settings_green);
textView_fourth.setTextColor(getResources().getColor(R.color.colorViewPress));
break;
default:
break;
}
}
//重置選單的點擊狀態,設為未點擊
private void restartButton() {
//設定為未點擊狀態
//第一片段
imageView_first.setImageResource(R.drawable.chat);
textView_first.setTextColor(getResources().getColor(R.color.black));
//第二片段
imageView_second.setImageResource(R.drawable.contacts);
textView_second.setTextColor(getResources().getColor(R.color.black));
//第三片段
imageView_third.setImageResource(R.drawable.circle_people);
textView_third.setTextColor(getResources().getColor(R.color.black));
//第四片段
imageView_fourth.setImageResource(R.drawable.settings);
textView_fourth.setTextColor(getResources().getColor(R.color.black));
}
//初始化中間的部分的圖層片段
private void initFragment(){
fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.add(R.id.frame_content,fragment_first);
transaction.add(R.id.frame_content,fragment_second);
transaction.add(R.id.frame_content,fragment_third);
transaction.add(R.id.frame_content,fragment_fourth);
//提交事務
transaction.commit();
}
//初始化各底端的LinearLayout、ImageView和TextView組件
private void initView(){
linear_first=findViewById(R.id.chat);
linear_second=findViewById(R.id.contacts);
linear_third=findViewById(R.id.circle_friend);
linear_fourth=findViewById(R.id.settings);
imageView_first=findViewById(R.id.imageView1);
imageView_second=findViewById(R.id.imageView2);
imageView_third=findViewById(R.id.imageView3);
imageView_fourth=findViewById(R.id.imageView4);
textView_first=findViewById(R.id.textView1);
textView_second=findViewById(R.id.textView2);
textView_third=findViewById(R.id.textView3);
textView_fourth=findViewById(R.id.textView4);
}
//初始化點擊監聽事件
private void initEvent(){
linear_first.setOnClickListener(this);
linear_second.setOnClickListener(this);
linear_third.setOnClickListener(this);
linear_fourth.setOnClickListener(this);
}
//隱藏所有圖層分段
private void hideView(FragmentTransaction transaction){
transaction.hide(fragment_first);
transaction.hide(fragment_second);
transaction.hide(fragment_third);
transaction.hide(fragment_fourth);
}
//選擇相應的圖層分段
private void selectFragment(int i){
FragmentTransaction transaction=fragmentManager.beginTransaction();
//呼叫隱藏所有圖層函式
hideView(transaction);
switch (i){
case 0:
transaction.show(fragment_first);
break;
case 1:
transaction.show(fragment_second);
break;
case 2:
transaction.show(fragment_third);
break;
case 3:
transaction.show(fragment_fourth);
break;
default:
break;
}
//提交轉換事務
transaction.commit();
}
}
小結:在Android Studio中進行仿微信APP門戶界面的初步設計,實作了top和bottom導航欄的設計,及中間片段圖層之間的隱藏與切換,最后實作了點擊選單后的顏色切換問題,用到了TextView、ImageView、LinearLayout、Fragment組件,了解了點擊事件onClick函式的撰寫,
具體代碼已上傳至gitee代碼倉庫
——2021.10.04
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/305497.html
標籤:其他
下一篇:Android 網路開發
