Android項(xiàng)目實(shí)戰(zhàn)之仿網(wǎng)易頂部導(dǎo)航欄效果
隨著時(shí)間的推移現(xiàn)在的軟件要求顯示的內(nèi)容越來(lái)越多,所以要在小的屏幕上能夠更好的顯示更多的內(nèi)容,首先我們會(huì)想到底部菜單欄,但是有時(shí)候想網(wǎng)易新聞要顯示的內(nèi)容太多,而且又想在主頁(yè)面全部顯示出來(lái),所以有加了頂部導(dǎo)航欄,但是Android這樣的移動(dòng)設(shè)備內(nèi)存是受限的,那么多界面緩存到內(nèi)存中,很容易導(dǎo)致內(nèi)存溢出,這個(gè)是比較致命的,所以不得不考慮。雖然我在之前也做過(guò)網(wǎng)易的頂部導(dǎo)航欄但是方式并不好,就像使用viewpager做一些復(fù)雜的界面由于圖片占用內(nèi)存過(guò)多,很容易導(dǎo)致內(nèi)存溢出,學(xué)習(xí)了今天的內(nèi)容大家做一下對(duì)比相信就有所體會(huì)。<
先看一下今天要實(shí)現(xiàn)的效果:

至于頂部導(dǎo)航的具體要用到的圖片和布局大家自己調(diào)整。
由于前面已經(jīng)介紹了底部菜單欄了,所以一些重復(fù)性的代碼就不貼上來(lái)了,最后我也會(huì)把下載地址貼上大家有興趣自行下載。
首先看一些頂部導(dǎo)航欄的布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <include layout="@layout/head" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <RadioGroup android:id="@+id/add_tab_group" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="6dp" android:paddingBottom="6dp" android:background="@drawable/big_button_up" android:orientation="horizontal" > <RadioButton android:id="@+id/main_tab_addExam" style="@style/MMTabButton1" android:layout_weight="1.0" android:checked="true" android:text="添加考試" /> <RadioButton android:id="@+id/main_tab_myExam" style="@style/MMTabButton1" android:layout_weight="1.0" android:text="我的考試" /> <RadioButton android:id="@+id/main_tab_message" style="@style/MMTabButton1" android:layout_weight="1.0" android:text="我的通知" /> <RadioButton android:id="@+id/main_tab_testing" style="@style/MMTabButton1" android:layout_weight="1.0" android:text="測(cè)試" /> <RadioButton android:id="@+id/main_tab_settings" style="@style/MMTabButton1" android:layout_weight="1.0" android:text="設(shè)置" /> </RadioGroup> </LinearLayout> <LinearLayout android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > </LinearLayout> </LinearLayout>
具體寬度樣式大家可以自己調(diào)節(jié),然后看一下核心類:
import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class AddExamActivity extends ActivityGroup {
protected Button btn_leftTop, btn_rightTop;
protected TextView tv_head;
private static LocalActivityManager manager;
private RadioGroup radioGroup;
private static LinearLayout container;
public static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addexam);
context=this;
initHead();
manager=getLocalActivityManager();
container= (LinearLayout)findViewById(R.id.container);
radioGroup=(RadioGroup) this.findViewById(R.id.add_tab_group);
container.removeAllViews();
container.addView(manager.startActivity(
"PAGE_0",
new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.main_tab_addExam://添加考試
container.removeAllViews();
container.addView(manager.startActivity(
"PAGE_0",
new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
break;
case R.id.main_tab_myExam://我的考試
container.removeAllViews();
container.addView(manager.startActivity(
"PAGE_1",
new Intent(context, MyMessageActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
break;
case R.id.main_tab_message://我的通知
container.removeAllViews();
container.addView(manager.startActivity(
"PAGE_2",
new Intent(context, SettingActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
break;
case R.id.main_tab_testing://測(cè)試
container.removeAllViews();
container.addView(manager.startActivity(
"PAGE_3",
new Intent(context, TestingActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
break;
case R.id.main_tab_settings://設(shè)置
container.removeAllViews();
container.addView(manager.startActivity(
"PAGE_4",
new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
break;
default:
//tabHost.setCurrentTabByTag("我的考試");
break;
}
}
});
}
public static void changeTo(){
Animation slideLeftIn = AnimationUtils.loadAnimation(context, R.anim.slide_bottom_in_no_alpha);
container.removeAllViews();
container.addView(manager.startActivity(
"PAGE_4",
new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
container.startAnimation(slideLeftIn);
}
protected void initHead() {
btn_leftTop = (Button) findViewById(R.id.btn_leftTop);
btn_rightTop = (Button) findViewById(R.id.btn_rightTop);
tv_head = (TextView) findViewById(R.id.tv_head);
btn_leftTop.setVisibility(View.INVISIBLE);
tv_head.setText("添加考試");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
builder.setMessage("你確定退出嗎?")
.setCancelable(false)
.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
finish();
System.exit(0);
}
})
.setNegativeButton("返回",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
這里繼承了ActivityGroup,沒(méi)有使用過(guò)的朋友從百度搜索下就明白了。
使用了LocalActivityManager啟動(dòng)子activity,這里Context和LinearLayout使用了static靜態(tài)的,這是因?yàn)樽儜B(tài)的需求使我不得不這樣做,希望大家不要把這兩個(gè)變量設(shè)置成static的,因?yàn)閟tatic的生命周期很長(zhǎng)特別是Context不要設(shè)置成static,這樣的話當(dāng)前的activity很難被銷毀的。其實(shí)使用tabhost完全可以實(shí)現(xiàn),但是為什么沒(méi)使用tabhost的我相信大家都明白,如果不考慮內(nèi)存的話我也會(huì)使用。
以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android程序開發(fā)之Fragment實(shí)現(xiàn)底部導(dǎo)航欄實(shí)例代碼
- Android實(shí)現(xiàn)沉浸式通知欄通知欄背景顏色跟隨app導(dǎo)航欄背景顏色而改變
- Android實(shí)現(xiàn)底部導(dǎo)航欄功能(選項(xiàng)卡)
- Android 沉浸式狀態(tài)欄與隱藏導(dǎo)航欄實(shí)例詳解
- Android仿網(wǎng)易客戶端頂部導(dǎo)航欄效果
- 超簡(jiǎn)單的幾行代碼搞定Android底部導(dǎo)航欄功能
- Android實(shí)現(xiàn)頂部導(dǎo)航欄可點(diǎn)擊可滑動(dòng)效果(仿微信仿豆瓣網(wǎng))
- Android仿今日頭條頂部導(dǎo)航欄效果的實(shí)例代碼
- Android實(shí)現(xiàn)簡(jiǎn)單底部導(dǎo)航欄 Android仿微信滑動(dòng)切換效果
- Android自定義字母導(dǎo)航欄
相關(guān)文章
Android巧用ViewPager實(shí)現(xiàn)左右循環(huán)滑動(dòng)圖片
這篇文章主要為大家詳細(xì)介紹了Android巧用ViewPager實(shí)現(xiàn)左右循環(huán)滑動(dòng)圖片的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
Android組件banner實(shí)現(xiàn)左右滑屏效果
這篇文章主要為大家詳細(xì)介紹了Android組件banner實(shí)現(xiàn)左右滑屏效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
詳解Android App卸載后跳轉(zhuǎn)到指定的反饋?lái)?yè)面的方法
這篇文章主要介紹了Android App卸載后跳轉(zhuǎn)到指定的反饋?lái)?yè)面的方法,關(guān)鍵點(diǎn)是相關(guān)線程要判斷在目錄被消除以前作出響應(yīng),需要的朋友可以參考下2016-04-04
Android開發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問(wèn)題
這篇文章主要介紹了Android開發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問(wèn)題的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)
這篇文章主要介紹了Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Android RecyclerView item選中放大被遮擋問(wèn)題詳解
這篇文章主要介紹了Android RecyclerView item選中放大被遮擋問(wèn)題詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04

