Android仿qq頂部消息欄效果
android仿照qq的頂部欄效果,主要就是利用fragment manager把fragment設(shè)置顯示內(nèi)容
(1)在activity_main.xml布局中添加控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:id="@+id/ll_qqtop"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center"
android:background="@color/whites">
<LinearLayout
android:id="@+id/common_constact"
android:layout_height="40dp"
android:layout_width="150dp"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true">
<Button
android:id="@+id/constact_group"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="5dp"
android:textSize="16sp"
android:button="@null"
android:checked="true"
android:background="@drawable/qq_contact_group"
android:textColor="@drawable/qq_constact_font"
android:text="消息"/>
<Button
android:button="@null"
android:id="@+id/constact_all"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="16sp"
android:padding="5dp"
android:background="@drawable/qq_contact_all"
android:textColor="@drawable/qq_constact_font"
android:text="電話"/>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/id_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/ll_qqtop" />
</RelativeLayout>
(2)在drawable中添加樣式文件,包括字體顏色和背景
2.1.在drawable文件夾中新建一個(gè)文件:qq_contact_group.xml,這個(gè)是左邊按鈕的背景樣式xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"><shape>
<corners android:bottomLeftRadius="5dp" android:bottomRightRadius="0dp" android:topLeftRadius="5dp" android:topRightRadius="0dp" />
<solid android:color="@color/blue" />
<stroke android:width="1dp" android:color="@color/blue" />
</shape>
</item>
<item android:state_pressed="true"><shape>
<corners android:bottomLeftRadius="5dp" android:bottomRightRadius="0dp" android:topLeftRadius="5dp" android:topRightRadius="0dp" />
<solid android:color="@color/whites" />
<stroke android:width="1dp" android:color="@color/whites" />
</shape>
</item>
<item><shape>
<corners android:bottomLeftRadius="5dp" android:bottomRightRadius="0dp" android:topLeftRadius="5dp" android:topRightRadius="0dp" />
<solid android:color="@color/whites" />
<stroke android:width="1dp" android:color="@color/blue" />
</shape>
</item>
</selector>
2.2在drawable文件夾中新建一個(gè)文件:qq_contact_all.xml,這個(gè)是右邊按鈕的背景樣式xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"><shape>
<corners android:bottomLeftRadius="0dp" android:bottomRightRadius="5dp" android:topLeftRadius="0dp" android:topRightRadius="5dp" />
<solid android:color="@color/blue" />
<stroke android:width="1dp" android:color="@color/blue" />
</shape>
</item>
<item android:state_pressed="true"><shape>
<corners android:bottomLeftRadius="0dp" android:bottomRightRadius="5dp" android:topLeftRadius="0dp" android:topRightRadius="5dp" />
<solid android:color="@color/blue" />
<stroke android:width="1dp" android:color="@color/blue" />
</shape>
</item>
<item><shape>
<corners android:bottomLeftRadius="0dp" android:bottomRightRadius="5dp" android:topLeftRadius="0dp" android:topRightRadius="5dp" />
<solid android:color="@color/whites" />
<stroke android:width="1dp" android:color="@color/blue" />
</shape>
</item>
</selector>
3.在drawable文件夾中新建一個(gè)文件:qq_constact_font.xml,這個(gè)是兩個(gè)按鈕的文字樣式xml,不選中為白色,選中為藍(lán)色
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:color="@color/whites"/> <item android:state_enabled="false" android:color="@color/whites"/> <item android:color="@color/blue"/> </selector>
(3)在MainActivity中設(shè)置按鈕的選中情況,并且在fragmentManager中調(diào)用fragment
public class MainActivity extends Activity implements View.OnClickListener {
//參考網(wǎng)址:https://blog.csdn.net/u010585448/article/details/48543883
private Button title_left_btn , title_right_btn;
/**
* Fragment管理器
*/
private android.app.FragmentManager mFragmentManager;
private FragmentTransaction mTransaction;
/**
* 兩個(gè)Fragment
*/
private LeftFragment mLFragment ;
private RightFragment mRFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
// TODO Auto-generated method stub
title_left_btn = (Button)findViewById(R.id.constact_group);
title_right_btn = (Button)findViewById(R.id.constact_all);
title_left_btn.setOnClickListener(this);
title_left_btn.performClick();//模擬點(diǎn)擊事件,使左邊按鈕被點(diǎn)擊
mFragmentManager = getFragmentManager();
mTransaction = mFragmentManager.beginTransaction();
mLFragment = new LeftFragment();
mTransaction.replace(R.id.id_content, mLFragment);
mTransaction.commit();
title_right_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.constact_group:
if(title_left_btn.isEnabled()){
title_left_btn.setEnabled(false);
title_right_btn.setEnabled(true);
}
mFragmentManager = getFragmentManager();
mTransaction = mFragmentManager.beginTransaction();
if(mLFragment == null){
mLFragment = new LeftFragment();
}
mTransaction.replace(R.id.id_content, mLFragment);
mTransaction.commit();
break;
case R.id.constact_all:
if(title_right_btn.isEnabled()){
title_left_btn.setEnabled(true);
title_right_btn.setEnabled(false);
}
mFragmentManager = getFragmentManager();
mTransaction = mFragmentManager.beginTransaction();
if(mRFragment == null){
mRFragment = new RightFragment();
}
mTransaction.replace(R.id.id_content, mRFragment);
mTransaction.commit();
break;
}
}
}
最后,簡(jiǎn)單貼一下fragment吧
public class LeftFragment extends android.app.Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.left_fragment, container , false);
}
}
實(shí)現(xiàn)效果圖:

總結(jié)
以上所述是小編給大家介紹的Android仿qq頂部消息欄效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問題
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
TextView實(shí)現(xiàn)圖文混合編排的方法
這篇文章主要為大家詳細(xì)介紹了TextView實(shí)現(xiàn)圖文混合編排的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Flutter源碼分析之自定義控件(RenderBox)指南
寫了兩天的flutter,發(fā)現(xiàn)控件樣式很多,flutter資源很少,本文在于實(shí)用性,可以減少頁面代碼,下面這篇文章主要介紹了Flutter源碼分析之自定義控件(RenderBox)的相關(guān)資料,需要的朋友可以參考下2021-08-08
android利用ContentResolver訪問者獲取手機(jī)聯(lián)系人信息
這篇文章主要介紹了android利用ContentResolver訪問者獲取手機(jī)聯(lián)系人信息,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
Android中Activity的四種啟動(dòng)模式和onNewIntent()
android 中activity的啟動(dòng)模式分為四種,(standard、singleTop、singTask、singleInstance),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08
Android進(jìn)階NestedScroll嵌套滑動(dòng)機(jī)制實(shí)現(xiàn)吸頂效果詳解
這篇文章主要為大家介紹了Android進(jìn)階NestedScroll嵌套滑動(dòng)機(jī)制實(shí)現(xiàn)吸頂效果詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android 開發(fā)中根據(jù)搜索內(nèi)容實(shí)現(xiàn)TextView中的文字部分加粗
最近遇到一個(gè)需求,需要做一個(gè)搜索功能。搜索的內(nèi)容需要加粗顯示。實(shí)現(xiàn)方法很簡(jiǎn)單,下面通過本文給大家分享Android 開發(fā)中根據(jù)搜索內(nèi)容實(shí)現(xiàn)TextView中的文字部分加粗樣式,非常不錯(cuò),需要的朋友參考下2017-03-03

