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;
}
}
}
最后,簡單貼一下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頂部消息欄效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android編程滑動(dòng)效果之Gallery+GridView實(shí)現(xiàn)圖片預(yù)覽功能(附demo源碼下載)
這篇文章主要介紹了Android編程滑動(dòng)效果之Gallery+GridView實(shí)現(xiàn)圖片預(yù)覽功能,結(jié)合實(shí)例形式分析了Android通過GridView和Gallery兩個(gè)控件模仿Gallery圖像集圖片預(yù)覽功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02
Android 中使用 AsyncTask 異步讀取網(wǎng)絡(luò)圖片
這篇文章主要介紹了Android 中使用 AsyncTask 異步讀取網(wǎng)絡(luò)圖片的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android進(jìn)程間大數(shù)據(jù)通信LocalSocket詳解
這篇文章主要為大家介紹了Android進(jìn)程間大數(shù)據(jù)通信LocalSocket詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android實(shí)現(xiàn)果凍滑動(dòng)效果的控件
這篇文章給大家主要介紹了利用Android如何實(shí)現(xiàn)果凍效果滑動(dòng)效果的控件,實(shí)現(xiàn)的效果類似于iOS有阻尼效果的滑動(dòng)控件,一般我們比較親切地稱之為果凍控件,常見的如微信里[我]的那個(gè)面板模塊,即使沒有再多的選項(xiàng),也不會(huì)很生硬的不允許用戶滑動(dòng)。下面來一起看看吧。2016-11-11
Android開發(fā)Kotlin語言協(xié)程中的并發(fā)問題和互斥鎖
Android開發(fā)Kotlin語言提供了多種機(jī)制來處理并發(fā)和同步,其中包括高層次和低層次的工具,對于常規(guī)的并發(fā)任務(wù),可以利用 Kotlin 協(xié)程提供的結(jié)構(gòu)化并發(fā)方式,而對于需要更低層次的鎖定機(jī)制,可以使用Mutex(互斥鎖)來實(shí)現(xiàn)對共享資源的線程安全訪問2024-06-06

