Android Studio實現(xiàn)仿微信APP門戶界面詳解及源碼
前言
你好! 本文章主要介紹如何用Android Studio制作簡易的門戶界面,主要說明框架的各部分功能與實現(xiàn)過程,結(jié)尾處附有源碼。
界面分析
注:按鈕圖標是從阿里矢量圖標庫獲取,保存在drawable文件中調(diào)用。
首先根據(jù)我們的大致規(guī)劃布局,我們可以先建立三個核心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文件切換。
界面動態(tài)實現(xiàn)代碼
目錄結(jié)構(gòu):
MainActivity:
建立相關(guān)變量:
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;
主要函數(shù)方法:
OnCreate: 利用我們在XML文件中定義的View的id屬性來獲取相應的View對象,并且加上View.OnClickListener接口,使下方生成的OnClick()方法自動匹配相應,同時在此函數(shù)中我們有添加了相應的監(jiān)聽器。
initFragment:
我們?yōu)榱藢崿F(xiàn)界面切換,需定義Fragment文件,因為我們的轉(zhuǎn)換界面有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函數(shù)應根據(jù)我們自己配置的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); } }
我們可以看到,此函數(shù)的返回值是根據(jù)XML文件而作出改變,如果忽視,界面轉(zhuǎn)換將會失敗。
initFragment函數(shù)主要作用就是向之前的lacitivity_main.xml文件中的Fragment部分添加我們要做切換的代碼。
showfagment: 我們在此函數(shù)中通過調(diào)用索引值來設置相應的界面效果代碼,例如Fragment界面展示、圖片的改變、字體的設置。(由于我選擇的按鈕圖片顏色過于鮮艷,故無法實現(xiàn)點擊時的亮暗轉(zhuǎn)換,為了體現(xiàn)按鈕被點擊,我設置了當點擊按鈕時字體顏色會發(fā)生變化作為替代)
hideFragment:顧名思義,此函數(shù)是為了隱藏Fragment,配合showFragment函數(shù)只顯示我們目前需要顯示的Fragment。
onClick:前面在介紹OnCreate函數(shù)時說過,是由View.OnClickListener接口生成,設置我們的點擊過程,并且此函數(shù)調(diào)用showFragment,完全控制我們制作的界面轉(zhuǎn)換流程。
靜態(tài)界面實現(xiàn)
目錄結(jié)構(gòu):
三個核心文件在前面已經(jīng)介紹過,在此不做過多解釋,如果不清楚可翻到上面去查看。根據(jù)上述創(chuàng)建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文件聯(lián)系起來,我們的XML文件也應與Fragment的java文件聯(lián)系起來。
以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>
調(diào)用context屬性與其JAVA文件聯(lián)系。
總結(jié)
本文介紹了AndriodStudio制作門戶界面的大致流程以及界面切換的功能,如有錯誤,敬請指正。
代碼倉庫:github
碼云鏈接:https://github.com/Haru-Malfoy/work1.git
到此這篇關(guān)于Android Studio實現(xiàn)仿微信APP門戶界面詳解及源碼的文章就介紹到這了,更多相關(guān)Android 微信界面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android textview 實現(xiàn)長按自由選擇復制功能的方法
下面小編就為大家?guī)硪黄狝ndroid textview 實現(xiàn)長按自由選擇復制功能的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Android編程之canvas繪制各種圖形(點,直線,弧,圓,橢圓,文字,矩形,多邊形,曲線,圓角矩形)
這篇文章主要介紹了Android編程之canvas繪制各種圖形的方法,涉及Android使用Canvas類中常用繪圖方法的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12詳解Android Studio中Git的配置及協(xié)同開發(fā)
這篇文章主要介紹了詳解Android Studio中Git的配置及協(xié)同開發(fā),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03Android 6.0開發(fā)實現(xiàn)關(guān)機菜單添加重啟按鈕的方法
這篇文章主要介紹了Android 6.0開發(fā)實現(xiàn)關(guān)機菜單添加重啟按鈕的方法,涉及Android6.0針對相關(guān)源碼的修改與功能添加操作技巧,需要的朋友可以參考下2017-09-09Android編程解析XML文件的方法詳解【基于XmlPullParser】
這篇文章主要介紹了Android編程解析XML文件的方法,結(jié)合實例形式分析了Android基于XmlPullParser解析xml文件的相關(guān)操作技巧與注意事項,需要的朋友可以參考下2017-07-07