溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

android開發(fā)中如何實現(xiàn)底部導(dǎo)航

發(fā)布時間:2022-01-17 17:58:45 來源:億速云 閱讀:312 作者:小新 欄目:移動開發(fā)

小編給大家分享一下 android開發(fā)中如何實現(xiàn)底部導(dǎo)航,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

使用LinearLayout 底部布局+p_w_picpathView 實現(xiàn)

     底部四個主導(dǎo)航頁面 布局文件  activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/container"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    tools:context="com.example.tastelibrary.MainActivity"  
    tools:ignore="MergeRootFrame" >  
  
    <FrameLayout  
        android:id="@+id/fl_content"  
        android:layout_width="match_parent"  
        android:layout_height="0dp"  
        android:layout_weight="1" >  
    </FrameLayout>  
  
    <LinearLayout  
        android:id="@+id/ll"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:orientation="horizontal" >  
  
        <LinearLayout  
            android:id="@+id/ll_recipe"  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:layout_weight="1"  
            android:gravity="center"  
            android:orientation="vertical" >  
  
            <ImageView  
                android:id="@+id/p_w_picpath_recipe"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:background="@drawable/recipe_btn_selector" />  
            
        </LinearLayout>  
  
        <LinearLayout  
            android:id="@+id/ll_kitchen"  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:layout_weight="1"  
            android:gravity="center"  
            android:orientation="vertical" >  
  
            <ImageView  
                android:id="@+id/p_w_picpath_kitchen"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:background="@drawable/kitchen_btn_selector" />  
        </LinearLayout>  
  
        <LinearLayout  
            android:id="@+id/ll_find"  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:layout_weight="1"  
            android:gravity="center"  
            android:orientation="vertical" >  
  
              <ImageView  
                android:id="@+id/p_w_picpath_find"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:background="@drawable/find_btn_selector" />  
        </LinearLayout>  
  
        <LinearLayout  
            android:id="@+id/ll_user"  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:layout_weight="1"  
            android:gravity="center"  
            android:orientation="vertical" >  
  
            <ImageView  
                android:id="@+id/p_w_picpath_user"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:background="@drawable/user_btn_selector" />  
        </LinearLayout>  
    </LinearLayout>  
  
</LinearLayout>

MainActivity

package com.example.tastelibrary;  
  
import android.support.v7.app.ActionBarActivity;  
import android.support.v7.app.ActionBar;  
import android.support.v4.app.Fragment;  
import android.support.v4.app.FragmentManager;  
import android.support.v4.app.FragmentTransaction;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.Menu;  
import android.view.MenuItem;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup;  
import android.widget.ImageView;  
import android.widget.LinearLayout;  
import android.os.Build;  
  
public class MainActivity extends ActionBarActivity  implements OnClickListener{  
     private LinearLayout ll_recipe;    
        private LinearLayout ll_kitchen;    
        private LinearLayout ll_find;    
        private LinearLayout ll_user;    
        private ImageView p_w_picpath_home;    
        private ImageView p_w_picpath_friends;    
        private ImageView p_w_picpath_message;    
        private ImageView p_w_picpath_more;    
        //Fragment管理器    
        private FragmentManager fm = this.getSupportFragmentManager();    
        private FragmentTransaction ft;    
        private RecipeFragment fragmentPage1;    
        private FindFragment fragmentPage2;    
        private KitchenFragment fragmentPage3;    
        private UserFragment fragmentPage4;    
        ActionBar myActionBar;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
      myActionBar=getSupportActionBar();  
        initView();    
        //開始事務(wù)(每次改變Fragment管理器之后都要提交)    
        ft = fm.beginTransaction();    
        home();    
        //提交事務(wù)    
        ft.commit();   
  
    }  
    private void initView(){    
        ll_recipe = (LinearLayout)findViewById(R.id.ll_recipe);    
        ll_kitchen = (LinearLayout)findViewById(R.id.ll_kitchen);    
        ll_find = (LinearLayout)findViewById(R.id.ll_find);    
        ll_user = (LinearLayout)findViewById(R.id.ll_user);    
            
        p_w_picpath_home = (ImageView)findViewById(R.id.p_w_picpath_recipe);    
        p_w_picpath_friends = (ImageView)findViewById(R.id.p_w_picpath_kitchen);    
        p_w_picpath_message = (ImageView)findViewById(R.id.p_w_picpath_find);    
        p_w_picpath_more = (ImageView)findViewById(R.id.p_w_picpath_user);    
            
        ll_recipe.setOnClickListener(this);    
        ll_kitchen.setOnClickListener(this);    
        ll_find.setOnClickListener(this);    
        ll_user.setOnClickListener(this);    
        ll_recipe.setSelected(true);    
        p_w_picpath_home.setSelected(true);    
            
    }    
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
          
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu);  
        return true;  
    }  
  
    @Override  
    public boolean onOptionsItemSelected(MenuItem item) {  
        int id = item.getItemId();  
        if (id == R.id.action_settings) {  
            return true;  
        }  
        return super.onOptionsItemSelected(item);  
    }  
  
  
    @Override  
    public void onClick(View v) {  
         //每次點擊時都需要重新開始事務(wù)    
        ft = fm.beginTransaction();    
        //把顯示的Fragment隱藏    
        setSelected();    
        switch (v.getId()) {    
        case R.id.ll_recipe:    
            ll_recipe.setSelected(true);    
            p_w_picpath_home.setSelected(true);    
            home();    
            break;    
        case R.id.ll_kitchen:    
            ll_kitchen.setSelected(true);    
            p_w_picpath_friends.setSelected(true);    
            friend();    
                
            break;    
        case R.id.ll_find:    
            ll_find.setSelected(true);    
            p_w_picpath_message.setSelected(true);    
            message();    
            break;    
        case R.id.ll_user:    
            ll_user.setSelected(true);    
            p_w_picpath_more.setSelected(true);    
            more();    
            break;    
        }    
        ft.commit();    
    }  
    /** 
     * 設(shè)置每個按鈕是否被選中 
     */  
    private void setSelected(){    
        ll_recipe.setSelected(false);    
        ll_kitchen.setSelected(false);    
        ll_find.setSelected(false);    
        ll_user.setSelected(false);    
        p_w_picpath_home.setSelected(false);    
        p_w_picpath_friends.setSelected(false);    
        p_w_picpath_message.setSelected(false);    
        p_w_picpath_more.setSelected(false);    
        if(fragmentPage1 != null){    
            //隱藏Fragment    
            ft.hide(fragmentPage1);    
        }    
        if(fragmentPage2 != null){    
            ft.hide(fragmentPage2);    
        }    
        if(fragmentPage3 != null){    
            ft.hide(fragmentPage3);    
        }    
        if(fragmentPage4 != null){    
            ft.hide(fragmentPage4);    
        }    
    }    
    private void home(){    
        if(fragmentPage1 == null){    
            fragmentPage1 = new RecipeFragment();    
            /*添加到Fragment管理器中  
            這里如果用replace,  
            當(dāng)每次調(diào)用時都會把前一個Fragment給干掉,  
            這樣就導(dǎo)致了每一次都要創(chuàng)建、銷毀,  
            數(shù)據(jù)就很難保存,用add就不存在這樣的問題了,  
            當(dāng)Fragment存在時候就讓它顯示,不存在時就創(chuàng)建,  
            這樣的話數(shù)據(jù)就不需要自己保存了,  
            因為第一次創(chuàng)建的時候就已經(jīng)保存了,  
            只要不銷毀一直都將存在*/    
            ft.add(R.id.fl_content, fragmentPage1);    
        }else{    
            //顯示Fragment    
            ft.show(fragmentPage1);    
        }    
    }    
    private void friend(){    
        if(fragmentPage2 == null){    
            fragmentPage2 = new FindFragment();    
            ft.add(R.id.fl_content, fragmentPage2);    
        }else{    
            ft.show(fragmentPage2);    
        }    
            
    }    
    private void message(){    
        if(fragmentPage3 == null){    
            fragmentPage3 = new KitchenFragment();    
            ft.add(R.id.fl_content, fragmentPage3);    
        }else{    
            ft.show(fragmentPage3);    
        }    
            
    }    
    private void more(){    
        if(fragmentPage4 == null){    
            fragmentPage4 = new UserFragment();    
            ft.add(R.id.fl_content, fragmentPage4);    
        }else{    
            ft.show(fragmentPage4);    
        }    
            
    }    
}

以上是“ android開發(fā)中如何實現(xiàn)底部導(dǎo)航”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI