android網易頂部導覽列demo

來源:互聯網
上載者:User

             隨著時間的推移現在的軟體要求顯示的內容越來越多,所以要在小的螢幕上能夠更好的顯示更多的內容,首先我們會想到底部功能表列,但是有時候想網易新聞要顯示的內容太多,而且又想在首頁面全部顯示出來,所以有加了頂部導覽列,但是android這樣的行動裝置記憶體是受限的,那麼多介面緩衝到記憶體中,很容易導致記憶體溢出,這個是比較致命的,所以不得不考慮。雖然我在之前也做過網易的頂部導覽列但是哪種方式並不好,就像使用viewpager做一些複雜的介面由於圖片佔用記憶體過多,很容易導致記憶體溢出,學習了今天的內容大家做一下對比相信就有所體會。

              先看一下今天要實現的效果:

     

          至於頂部導航的具體要用到的圖片和布局大家自己調整。

        由於前面已經介紹了底部功能表列了,所以一些重複性的代碼就不貼上來了,最後我也會把貼上大家有興趣自行下載。

        首先看一些頂部導覽列的布局檔案:

<?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="測試" />                <RadioButton                    android:id="@+id/main_tab_settings"                    style="@style/MMTabButton1"                    android:layout_weight="1.0"                    android:text="設定" />            </RadioGroup>                   </LinearLayout>           <LinearLayout        android:id="@+id/container"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="1" >    </LinearLayout></LinearLayout>

      具體寬度樣式大家可以自己調節,然後看一下核心類:

  

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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubswitch (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://測試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://設定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("添加考試");}   @Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubif (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,沒有使用過的朋友從百度搜尋下就明白了。

     使用了LocalActivityManager啟動子activity,這裡Context和LinearLayout使用了static靜態,這是因為變態的需求使我不得不這樣做,希望大家不要把這兩個變數設定成static的,因為static的生命週期很長特別是Context不要設定成static,這樣的話當前的activity很難被銷毀的。其實使用tabhost完全可以實現,但是為什麼沒使用tabhost的我相信大家都明白,如果不考慮記憶體的話我也會使用,哈哈!

    

      最後附上,有興趣大家自己下載吧!點擊開啟連結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.