android 开发 实例 下部主导航(1)

1.使用LinearLayout 底部布局+imageView 实现

     底部四个主导航页面 布局文件  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/image_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/image_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/image_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/image_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 image_home;    
        private ImageView image_friends;    
        private ImageView image_message;    
        private ImageView image_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();    
        //开始事务(每次改变Fragment管理器之后都要提交)    
        ft = fm.beginTransaction();    
        home();    
        //提交事务    
        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);    
            
        image_home = (ImageView)findViewById(R.id.image_recipe);    
        image_friends = (ImageView)findViewById(R.id.image_kitchen);    
        image_message = (ImageView)findViewById(R.id.image_find);    
        image_more = (ImageView)findViewById(R.id.image_user);    
            
        ll_recipe.setOnClickListener(this);    
        ll_kitchen.setOnClickListener(this);    
        ll_find.setOnClickListener(this);    
        ll_user.setOnClickListener(this);    
        ll_recipe.setSelected(true);    
        image_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) {  
         //每次点击时都需要重新开始事务    
        ft = fm.beginTransaction();    
        //把显示的Fragment隐藏    
        setSelected();    
        switch (v.getId()) {    
        case R.id.ll_recipe:    
            ll_recipe.setSelected(true);    
            image_home.setSelected(true);    
            home();    
            break;    
        case R.id.ll_kitchen:    
            ll_kitchen.setSelected(true);    
            image_friends.setSelected(true);    
            friend();    
                
            break;    
        case R.id.ll_find:    
            ll_find.setSelected(true);    
            image_message.setSelected(true);    
            message();    
            break;    
        case R.id.ll_user:    
            ll_user.setSelected(true);    
            image_more.setSelected(true);    
            more();    
            break;    
        }    
        ft.commit();    
    }  
    /** 
     * 设置每个按钮是否被选中 
     */  
    private void setSelected(){    
        ll_recipe.setSelected(false);    
        ll_kitchen.setSelected(false);    
        ll_find.setSelected(false);    
        ll_user.setSelected(false);    
        image_home.setSelected(false);    
        image_friends.setSelected(false);    
        image_message.setSelected(false);    
        image_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,  
            当每次调用时都会把前一个Fragment给干掉,  
            这样就导致了每一次都要创建、销毁,  
            数据就很难保存,用add就不存在这样的问题了,  
            当Fragment存在时候就让它显示,不存在时就创建,  
            这样的话数据就不需要自己保存了,  
            因为第一次创建的时候就已经保存了,  
            只要不销毁一直都将存在*/    
            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);    
        }    
            
    }    
}


本文出自 “李挺哲的安卓学习” 博客,请务必保留此出处http://2673720.blog.51cto.com/2663720/1565237

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。