基于Android Studio創建的仿微信APP門戶界面
- 前言
- 界面分析
- 界面動態實作代碼
- 靜態界面實作
- 總結
前言
你好! 本文章主要介紹如何用Android Studio制作簡易的門戶界面,主要說明框架的各部分功能與實作程序,結尾處附有原始碼,
界面分析
注:按鈕圖示是從阿里矢量圖示庫獲取,保存在drawable檔案中呼叫,

首先根據我們的大致規劃布局,我們可以先建立三個核心XML檔案:
top.xml:
<?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="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#070707"
android:gravity="center"
android:text="奶茶小樣"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="#F8F5F5"
android:textSize="26sp"
android:textStyle="bold"
android:typeface="monospace" />
</LinearLayout>
</LinearLayout>
bottom.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="100dp"
android:background="#0B0B0B"
android:baselineAligned="false"
android:gravity="center|center_horizontal">
<LinearLayout
android:id="@+id/bottom_zhenzhu_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|center_horizontal"
android:orientation="vertical">
<ImageButton
android:id="@+id/bottom_zhenzhu_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black"
android:clickable="false"
android:contentDescription="@string/app_name"
android:src="@drawable/zhenzhu" />
<!-- tools:srcCompat="@drawable/Zhengzhou" />-->
<TextView
android:id="@+id/bottom_zhenzhu_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="珍珠"
android:textColor="#FBFBFB"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_chadong_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageButton
android:id="@+id/bottom_chadong_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black"
android:clickable="false"
android:contentDescription="@string/app_name"
android:src="@drawable/milktea1"
tools:srcCompat="@drawable/milktea1" />
<TextView
android:id="@+id/bottom_chadong_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="茶凍"
android:textColor="#FBFAFA"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_naigai_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageButton
android:id="@+id/bottom_naigai_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black"
android:clickable="false"
android:contentDescription="@string/app_name"
android:src="@drawable/milktea2"
tools:srcCompat="@drawable/milktea2" />
<TextView
android:id="@+id/bottom_naigai_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="奶蓋"
android:textColor="#FBF8F8"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_buding_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageButton
android:id="@+id/bottom_buding_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black"
android:clickable="false"
android:contentDescription="@string/app_name"
android:src="@drawable/milktea3"
tools:srcCompat="@drawable/milktea3" />
<TextView
android:id="@+id/bottom_buding_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="布丁"
android:textColor="#FAF8F8"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
lactivity_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:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
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="match_parent"
android:layout_weight="1">
</FrameLayout>
<include
layout="@layout/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
注意:在top.xml和bottom.xml檔案寫好后,將其插入到lactivity_main.xml檔案的頭尾位置,并在中間加入FrameLayout來設定之后的Fragment檔案切換,
界面動態實作代碼
目錄結構:

MainActivity:
建立相關變數:
private Fragment zhenzhuFragment=new zhenzhuFragment();
private Fragment naigaiFragment=new naigaiFragment();
private Fragment budingFragment=new budingFragment();
private Fragment chadongFragment=new chadongFragment();
private FragmentManager fragmentManager;
private LinearLayout mTzhenzhu,mTchadong,mTnaigai,mTbuding;
private ImageButton mTmgZhenZhu;
private ImageButton mTmgChaDong;
private ImageButton mTmgNaiGai;
private ImageButton mTmgBuDing;
private TextView text_zhenzhu;
private TextView text_chadong;
private TextView text_naigai;
private TextView text_buding;
主要函式方法:

OnCreate: 利用我們在XML檔案中定義的View的id屬性來獲取相應的View物件,并且加上View.OnClickListener介面,使下方生成的OnClick()方法自動匹配相應,同時在此函式中我們有添加了相應的監聽器,
initFragment:
我們為了實作界面切換,需定義Fragment檔案,因為我們的轉換界面有4種,故我們總共需要5個fragment檔案,
wechatFragemt:
public class wechatFragment extends Fragment {
public wechatFragment() {
// 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_wechat, container, false);
}
}
其余四個檔案大致上與此檔案相似,但其中的onCreateView函式應根據我們自己配置的XML檔案而有所不同,例如:
budingFragment:
public class budingFragment extends Fragment {
public budingFragment(){
}
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.buding_fragment_wechat,container,false);
}
}
我們可以看到,此函式的回傳值是根據XML檔案而作出改變,如果忽視,界面轉換將會失敗,
initFragment函式主要作用就是向之前的lacitivity_main.xml檔案中的Fragment部分添加我們要做切換的代碼,
showfagment: 我們在此函式中通過呼叫索引值來設定相應的界面效果代碼,例如Fragment界面展示、圖片的改變、字體的設定,(由于我選擇的按鈕圖片顏色過于鮮艷,故無法實作點擊時的亮暗轉換,為了體現按鈕被點擊,我設定了當點擊按鈕時字體顏色會發生變化作為替代)
hideFragment:顧名思義,此函式是為了隱藏Fragment,配合showFragment函式只顯示我們目前需要顯示的Fragment,
onClick:前面在介紹OnCreate函式時說過,是由View.OnClickListener介面生成,設定我們的點擊程序,并且此函式呼叫showFragment,完全控制我們制作的界面轉換流程,
靜態界面實作
目錄結構:

三個核心檔案在前面已經介紹過,在此不做過多解釋,如果不清楚可翻到上面去查看,根據上述創建5個Fragment檔案,我們應對應生成5個Fragment的XML檔案來設計界面效果,
fragment_wechat:
此檔案是由上述的的Fragment的java檔案自動生成,故其余四個檔案可參考該檔案進行配置,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:orientation="vertical"
tools:context=".wechatFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="這是微信聊天界面"
android:textSize="48sp" />
</LinearLayout>
在此提醒,像我前面寫的Fragment的java檔案因與對應的XML檔案聯系起來,我們的XML檔案也應與Fragment的java檔案聯系起來,
以buding_fragment_wechat為例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".budingFragment">
<!-- tools:context=".wechatFragment"-->
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="這是布丁界面"
android:textSize="48sp" />
</LinearLayout>
呼叫context屬性與其JAVA檔案聯系,
總結
- 本文介紹了AndriodStudio制作門戶界面的大致流程以及界面切換的功能,如有錯誤,敬請指正,
- 代碼倉庫:github
- 碼云鏈接:https://github.com/Haru-Malfoy/work1.git
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/306461.html
標籤:其他
上一篇:再也不在簡歷上寫熟悉Glide,面試官一波連環炮整麻了!
下一篇:as微信界面設計
