Android高仿微信5.2.1主界面及消息提醒
好久沒更新博客了,最近在做公司的項(xiàng)目,這也算是我接觸的第一個(gè)正式項(xiàng)目。通過項(xiàng)目的檢驗(yàn),發(fā)現(xiàn)自己積累了一年的知識還是遠(yuǎn)遠(yuǎn)不夠,想要提高,好的方法是 :項(xiàng)目+書+視頻+博客。最重要一點(diǎn):勤動(dòng)手。最近發(fā)現(xiàn)了慕課網(wǎng)的視頻,居然都是高清無碼免費(fèi)的!而且滿滿的干貨!我用業(yè)余時(shí)間跟著視頻中大神的講解學(xué)習(xí)了不少知識,下面就將這些小demo與大家分享,當(dāng)然,我做了一些優(yōu)化,代碼與視頻中有些出入,但功能可以完全實(shí)現(xiàn)。
這是一個(gè)模仿5.2.1版本的顯示界面,如下圖所示:


功能及實(shí)現(xiàn)思路簡介
主要功能很簡單:
1、上面有一個(gè)自定義的標(biāo)題欄;
2、往下是聊天、發(fā)現(xiàn)、通訊錄選項(xiàng)卡;
3、手指滑動(dòng)時(shí),文字下方藍(lán)色的indicator可以跟隨滑動(dòng);
4、在聊天的右側(cè),有一個(gè)未讀消息的紅色提醒圓點(diǎn)。
自定義的標(biāo)題欄就是一個(gè)LinearLayout,同時(shí)將系統(tǒng)自帶的TitleBar(或是ActionBar)隱藏;
由于是選項(xiàng)卡,自然想到了Fragment;
手指可以滑動(dòng),顯然,黑色的區(qū)域是一個(gè)ViewPager,數(shù)據(jù)源就是Fragment組成的集合,并通過FragmentPagerAdapter進(jìn)行管理;
要實(shí)現(xiàn)藍(lán)色的indicator隨選項(xiàng)卡的滑動(dòng)而滑動(dòng),可以為ViewPager設(shè)置監(jiān)聽,并根據(jù)回調(diào)方法的回傳值控制該Indicator的marginLeft屬性值可以實(shí)現(xiàn)該效果。
最后消息提醒的小圓點(diǎn)是一個(gè)BadgeView ,它是一個(gè)第三方開源控件。
主布局
MainActivity布局如下,首先是自定義的TitleBar:
<!-- top1.xml --> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/topone_bg" android:paddingLeft="12dp" android:paddingRight="12dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:gravity="center" android:orientation="horizontal"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_icon" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:text="微信" android:textColor="#D3D3D3" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:orientation="horizontal"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_search_icon" /> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_add_icon" /> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_more_icon" /> </LinearLayout> </RelativeLayout>
效果如下所示:

接著是三個(gè)選項(xiàng)卡的布局:
<!-- top2.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="40dp"
android:background="#EEEEEE"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="37dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/ll_chat"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_tab_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="聊天"
android:textColor="#008000"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="@+id/tv_tab_discover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發(fā)現(xiàn)"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="@+id/tv_tab_contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="通訊錄"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/iv_tab_line"
android:layout_width="100dp"
android:layout_height="3dp"
android:background="@drawable/tabline" />
</LinearLayout>
效果如下:

由于Indicator還需要在代碼中動(dòng)態(tài)設(shè)置其長度,故在xml中可以附一個(gè)任意值。
最后將top1.xml、top2.xml加入至主布局中,并在主布局中引入ViewPager:
<!-- activity_main.xml --> <?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="com.demo.lenovo.myapplication.MainActivity"> <include layout="@layout/top1" /> <include layout="@layout/top2" /> <android.support.v4.view.ViewPager android:id="@+id/vp_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
效果如下:

注:如您的Activity繼承于ActionBarActivity,可以在setContentView()方法之前調(diào)用requestWindowFeature(Window.FEATURE_NO_TITLE);隱藏標(biāo)題欄;如繼承于AppCompactActivity,可以在AndroidMainfest
中的Application標(biāo)簽中設(shè)置主題為:android:theme="@style/Theme.AppCompat.NoActionBar",也可以實(shí)現(xiàn)隱藏標(biāo)題欄的目的。
使用FragmentPagerAdapter為ViewPager適配數(shù)據(jù)
在MainActivity.java 中,加入FragmentPagerAdapter邏輯:(在此略去三個(gè)Fragment的布局及代碼)
private FragmentPagerAdapter adapter;
private List<Fragment> mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
在initView()中,初始化Fragment,并將Fragment實(shí)例一次裝入List中,接著,在初始化FragmentPagerAdapter時(shí)管理List的數(shù)據(jù)。最后調(diào)用ViewPager的setAdapter方法將FragmentPagerAdapter實(shí)例傳入。
mData = new ArrayList<>();
mData.add(new ChatFragment());
mData.add(new DiscoverFragment());
mData.add(new ContactsFragment());
adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return mData.get(position);
}
@Override
public int getCount() {
return mData.size();
}
};
vp_content.setAdapter(adapter);
設(shè)置滑動(dòng)時(shí)字體顏色的變化
為實(shí)現(xiàn)該功能,需要為ViewPager設(shè)置setOnPageChangeListener監(jiān)聽,并通過OnPageChangeListener接口的回調(diào)方法onPagerSelected(int position),監(jiān)聽當(dāng)前滑動(dòng)到了第幾頁:
@Override
public void onPageSelected(int position) {
Log.e(TAG, "onPageSelected: " + position);
resetTextViewColor();
switch (position) {
case 0:
addBadgeView(); tv_tab_chat.setTextColor(Color.parseColor("#008000"));
break;
case 1:
tv_tab_discover.setTextColor(Color.parseColor("#008000"));
break;
case 2:
tv_tab_contacts.setTextColor(Color.parseColor("#008000"));
break;
}
}
//首先將每個(gè)選項(xiàng)卡的文字顏色置為黑色
private void resetTextViewColor() {
tv_tab_contacts.setTextColor(Color.BLACK);
tv_tab_chat.setTextColor(Color.BLACK);
tv_tab_discover.setTextColor(Color.BLACK);
添加BadgeView
在addBadgeView();方法中首先判斷BadgeView是否為空,若不為空,首先將其移除,再添加新的BadgeView,代碼如下:
private void addBadgeView()
{
if (mBadgeView != null) {
ll_chat.removeView(mBadgeView);
}
mBadgeView = new BadgeView(MainActivity.this);
ll_chat.addView(mBadgeView);
mBadgeView.setBadgeCount(9);
}
設(shè)置滑動(dòng)時(shí)字體顏色的變化
為實(shí)現(xiàn)該功能,需要為ViewPager設(shè)置setOnPageChangeListener監(jiān)聽,并通過OnPageChangeListener接口的回調(diào)方法onPagerSelected(int position),監(jiān)聽當(dāng)前滑動(dòng)到了第幾頁:
@Override
public void onPageSelected(int position) {
Log.e(TAG, "onPageSelected: " + position);
resetTextViewColor();
switch (position) {
case 0:
addBadgeView(); tv_tab_chat.setTextColor(Color.parseColor("#008000"));
break;
case 1:
tv_tab_discover.setTextColor(Color.parseColor("#008000"));
break;
case 2:
tv_tab_contacts.setTextColor(Color.parseColor("#008000"));
break;
}
}
//首先將每個(gè)選項(xiàng)卡的文字顏色置為黑色
private void resetTextViewColor() {
tv_tab_contacts.setTextColor(Color.BLACK);
tv_tab_chat.setTextColor(Color.BLACK);
tv_tab_discover.setTextColor(Color.BLACK);
添加BadgeView
在addBadgeView();方法中首先判斷BadgeView是否為空,若不為空,首先將其移除,再添加新的BadgeView,代碼如下:
private void addBadgeView()
{
if (mBadgeView != null) {
ll_chat.removeView(mBadgeView);
}
mBadgeView = new BadgeView(MainActivity.this);
ll_chat.addView(mBadgeView);
mBadgeView.setBadgeCount(9);
}
indicator的滑動(dòng)
為了實(shí)現(xiàn)該Indicator隨手指的滑動(dòng)而跟隨的效果,需要在OnPageChangeListener接口中的onPageScrolled()方法中編寫邏輯,該方法的文檔如下:
,
其中,第一個(gè)參數(shù)position表示滑動(dòng)到了第幾頁,比如說,若從第0頁滑動(dòng)至第一頁,那么position將一直為0,直到松手以后,滑動(dòng)至第一頁,position將變?yōu)?,第二個(gè)參數(shù)positionOffset表示滑動(dòng)的百分比,取值范圍是0-1,最后一個(gè)參數(shù)positionOffsetPixels表示滑動(dòng)的像素?cái)?shù)。
下面是從0—>1頁面時(shí)打印的log,如下所示:

從1—->2頁面時(shí)打印的log:

從2—->1頁面時(shí)打印的log:

最后,可以根據(jù)(position+positionOffset)*1/3,來設(shè)置該Indicator的marginLeft。
首先,應(yīng)為Indicator設(shè)置寬度,其寬度應(yīng)為屏幕寬度的1/3:
WindowManager manager = getWindow().getWindowManager();
Display display = manager.getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
mScreenOneThird = outMetrics.widthPixels / 3;
其中int型參數(shù)mScreenOneThird 的單位是像素px。
設(shè)置到Indicator上:
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv_tab_line.getLayoutParams();
lp.width = mScreenOneThird;
iv_tab_line.setLayoutParams(lp);
最終在onPageScrolled方法中動(dòng)態(tài)改變Indicator的marginLeft屬性:
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv_tab_line.getLayoutParams();
lp.leftMargin = (int) ((positionOffset * mScreenOneThird) + (mScreenOneThird * position));
iv_tab_line.setLayoutParams(lp);
可實(shí)現(xiàn)最終效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Android 7.0 Settings 加載選項(xiàng)
- android Setting中隱藏項(xiàng)實(shí)現(xiàn)原理與代碼
- Android編程實(shí)現(xiàn)下載時(shí)主界面與詳細(xì)界面一致更新的方法
- Android 中按home鍵和跳轉(zhuǎn)到主界面的實(shí)例代碼
- Android9.0 靜默安裝源碼的實(shí)現(xiàn)
- android異步消息機(jī)制 從源碼層面解析(2)
- android異步消息機(jī)制 源碼層面徹底解析(1)
- Android8.1 源碼修改之插入SIM卡默認(rèn)啟用Volte功能
- 分析Android 11.0Settings源碼之主界面加載
相關(guān)文章
Android nativePollOnce函數(shù)解析
這篇文章主要介紹了Android nativePollOnce函數(shù)解析的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
通過WIFI(不用數(shù)據(jù)線)連接Android手機(jī)調(diào)試
本文主要介紹WIFI 鏈接手機(jī)調(diào)試,這里詳細(xì)介紹了WIFI 鏈接Android手機(jī)實(shí)現(xiàn)調(diào)試的過程,有需要的小伙伴可以參考下2016-08-08
ListView的Adapter使用 之 初學(xué)ArrayAdapter String
ListView是Android中經(jīng)常會使用的東西,綁定數(shù)據(jù)對于初學(xué)者來說,尤其是剛接觸編程的人來說,往往會覺得很難理解,我上大二的時(shí)候?qū)W的java,但是基本上相當(dāng)于沒有學(xué),什么都沒寫過,真正接觸編程就是開始上手學(xué)android,把這些記錄下來,自己可以回頭看下,也可以讓新手更好的理解2013-06-06
Android Handler移除Message詳解及實(shí)例代碼
這篇文章主要介紹了Android Handler移除Message詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
android界面布局之實(shí)現(xiàn)文本塊布局效果示例
這篇文章主要介紹了android實(shí)現(xiàn)文本塊布局效果示例,需要的朋友可以參考下2014-04-04
Android自定義ScrollView實(shí)現(xiàn)放大回彈效果實(shí)例代碼
本篇文章主要介紹了Android自定義ScrollView實(shí)現(xiàn)放大回彈效果實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03
Android下拉刷新框架實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Android下拉刷新框架實(shí)現(xiàn)代碼實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11

