Android实现淘宝底部图标导航栏

在上一篇中,简单的使用透明主题的Activity实现了仿微信右侧顶部的对话框,上午又花了两个小时研究了一下淘宝底部的导航栏实现,网上的做法也有很多,今天我就使用一种通过基本控件加上布局的方式,没有任何的自定义风格,控件等来实现,还是老样子,先看一下效果图:

下面就来具体看一看如何实现的,还是按照步骤来吧:

绘制主界面

activity_layout.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  android:orientation="vertical"
  tools:context=".MainActivity">

  <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_marginBottom="50dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  </FrameLayout>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:background="@color/noCheckedColor">

    <RelativeLayout
      android:gravity="center"
      android:layout_width="wrap_content"
      android:layout_height="match_parent">
    <ImageView
      android:layout_marginTop="5dp"
      android:id="@+id/title_image"
      android:layout_marginLeft="18dp"
      android:layout_width="40dp"
      android:layout_height="40dp"
      android:background="@drawable/taobao" />

      <LinearLayout
        android:gravity="center"
        android:orientation="vertical"
        android:id="@+id/first_page_layout"
        android:layout_width="60dp"
        android:layout_height="wrap_content">
      <ImageView
        android:id="@+id/first_page_icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@drawable/firstpage" />

      <TextView
        android:textColor="#000000"
        android:id="@+id/first_page_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="首页" />
      </LinearLayout>

    </RelativeLayout>

      <LinearLayout
        android:layout_marginLeft="26dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="4">

        <LinearLayout
          android:id="@+id/micro_tao_layout"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:gravity="center"
          android:orientation="vertical">

          <ImageView
            android:id="@+id/microtao_icon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/microtao" />

          <TextView
            android:textColor="#000000"
            android:id="@+id/microtao_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="微淘" />
        </LinearLayout>

        <LinearLayout
          android:id="@+id/message_layout"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:gravity="center"
          android:orientation="vertical">

          <ImageView
            android:id="@+id/message_icon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/message" />

          <TextView
            android:textColor="#000000"
            android:id="@+id/message_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="消息" />
        </LinearLayout>

        <LinearLayout
          android:id="@+id/buycar_layout"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:gravity="center"
          android:orientation="vertical">

          <ImageView
            android:id="@+id/buycar_icon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/buycar" />

          <TextView
            android:textColor="#000000"
            android:id="@+id/buycar_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="购物车" />
        </LinearLayout>

        <LinearLayout
          android:id="@+id/myfile_layout"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:gravity="center"
          android:orientation="vertical">

          <ImageView
            android:id="@+id/myfile_icon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/myfile" />

          <TextView
            android:textColor="#000000"
            android:id="@+id/myfile_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="我的淘宝" />
        </LinearLayout>
      </LinearLayout>
    </LinearLayout>
</RelativeLayout>

界面的逻辑控制Activity:

MainActivity.java代码:

package com.hfut.enmulatetaobaonavbar;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hfut.enmulatetaobaonavbar.fragment.BuycarFragment;
import com.hfut.enmulatetaobaonavbar.fragment.FirstPageFragment;
import com.hfut.enmulatetaobaonavbar.fragment.MessageFragment;
import com.hfut.enmulatetaobaonavbar.fragment.MicroTaoFragment;
import com.hfut.enmulatetaobaonavbar.fragment.MyfileFragment;

/**
 * @author why
 * @date 2018-10-3 11:12:39
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  LinearLayout microTaoLay;
  LinearLayout messageLay;
  LinearLayout buycarLay;
  LinearLayout myfileLay;
  LinearLayout firstPageLay;

  ImageView microTaoIcon;
  ImageView messageIcon;
  ImageView buycarIcon;
  ImageView myfileIcon;
  ImageView firstPageIcon;
  ImageView titleImage;

  TextView microTaoText;
  TextView messageText;
  TextView buycarText;
  TextView myfileText;

  FragmentManager manager;
  FragmentTransaction transaction;
  Fragment firFragment, microFragment, messageFragment, buycarFragment, myfileFragment;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    manager = getSupportFragmentManager();
    transaction = manager.beginTransaction();
    firFragment = new FirstPageFragment();
    transaction.add(R.id.fragment_container, firFragment);
    transaction.commit();
    initUI();
  }

  private void initUI() {
    microTaoLay = findViewById(R.id.micro_tao_layout);
    messageLay = findViewById(R.id.message_layout);
    buycarLay = findViewById(R.id.buycar_layout);
    myfileLay = findViewById(R.id.myfile_layout);
    firstPageLay = findViewById(R.id.first_page_layout);
    firstPageLay.setVisibility(View.INVISIBLE);

    microTaoIcon = findViewById(R.id.microtao_icon);
    messageIcon = findViewById(R.id.message_icon);
    buycarIcon = findViewById(R.id.buycar_icon);
    myfileIcon = findViewById(R.id.myfile_icon);
    firstPageIcon = findViewById(R.id.first_page_icon);
    titleImage = findViewById(R.id.title_image);

    microTaoText = findViewById(R.id.microtao_text);
    messageText = findViewById(R.id.message_text);
    buycarText = findViewById(R.id.buycar_text);
    myfileText = findViewById(R.id.myfile_text);

    microTaoLay.setOnClickListener(this);
    messageLay.setOnClickListener(this);
    buycarLay.setOnClickListener(this);
    myfileLay.setOnClickListener(this);
    firstPageLay.setOnClickListener(this);

  }

  @Override
  public void onClick(View v) {
    transaction = manager.beginTransaction();
    hideFragment(transaction);//隐藏之前的Fragment
    switch (v.getId()) {
      case R.id.micro_tao_layout:
        microFragment = new MicroTaoFragment();
        transaction.add(R.id.fragment_container, microFragment);
        transaction.commit();
        microTaoIcon.setImageDrawable(getResources().getDrawable(R.drawable.microtao_choosen));
        microTaoText.setTextColor(Color.RED);

        //显示首页布局,隐藏标题淘宝图片
        if (firstPageLay.getVisibility() != View.VISIBLE) {
          firstPageLay.setVisibility(View.VISIBLE);
          titleImage.setVisibility(View.INVISIBLE);
        }

        break;
      case R.id.message_layout:
        messageFragment = new MessageFragment();
        transaction.add(R.id.fragment_container, messageFragment);
        transaction.commit();
        messageIcon.setImageDrawable(getResources().getDrawable(R.drawable.message_choosen));
        messageText.setTextColor(Color.RED);

        //显示首页布局,隐藏标题淘宝图片
        if (firstPageLay.getVisibility() != View.VISIBLE) {
          firstPageLay.setVisibility(View.VISIBLE);
          titleImage.setVisibility(View.INVISIBLE);
        }
        break;
      case R.id.buycar_layout:
        buycarFragment = new BuycarFragment();
        transaction.add(R.id.fragment_container, buycarFragment);
        transaction.commit();
        buycarIcon.setImageDrawable(getResources().getDrawable(R.drawable.buycar_choosen));
        buycarText.setTextColor(Color.RED);

        //显示首页布局,隐藏标题淘宝图片
        if (firstPageLay.getVisibility() != View.VISIBLE) {
          firstPageLay.setVisibility(View.VISIBLE);
          titleImage.setVisibility(View.INVISIBLE);
        }
        break;
      case R.id.myfile_layout:
        myfileFragment = new MyfileFragment();
        transaction.add(R.id.fragment_container, myfileFragment);
        transaction.commit();
        myfileIcon.setImageDrawable(getResources().getDrawable(R.drawable.myfile_choosen));
        myfileText.setTextColor(Color.RED);

        //显示首页布局,隐藏标题淘宝图片
        if (firstPageLay.getVisibility() != View.VISIBLE) {
          firstPageLay.setVisibility(View.VISIBLE);
          titleImage.setVisibility(View.INVISIBLE);
        }
        break;

      case R.id.first_page_layout:
        firFragment = new FirstPageFragment();
        transaction.add(R.id.fragment_container, firFragment);
        transaction.commit();
        firstPageLay.setVisibility(View.INVISIBLE);
        titleImage.setVisibility(View.VISIBLE);

      default:
        break;
    }
  }

  private void hideFragment(FragmentTransaction transaction) {
    if (firFragment != null) {
      transaction.remove(firFragment);

    }
    if (microFragment != null) {
      transaction.remove(microFragment);
      microTaoIcon.setImageDrawable(getResources().getDrawable(R.drawable.microtao));
      microTaoText.setTextColor(Color.BLACK);

    }
    if (messageFragment != null) {
      transaction.remove(messageFragment);
      messageIcon.setImageDrawable(getResources().getDrawable(R.drawable.message));
      messageText.setTextColor(Color.BLACK);
    }
    if (buycarFragment != null) {
      transaction.remove(buycarFragment);
      buycarIcon.setImageDrawable(getResources().getDrawable(R.drawable.buycar));
      buycarText.setTextColor(Color.BLACK);
    }
    if (myfileFragment != null) {
      transaction.remove(myfileFragment);
      myfileIcon.setImageDrawable(getResources().getDrawable(R.drawable.myfile));
      myfileText.setTextColor(Color.BLACK);
    }
  }
}

Fragment对应的布局代码(这里为了简化,所有Fragment使用的是同一个很简单的布局):
fragment_layout.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:gravity="center"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView
    android:gravity="center"
    android:id="@+id/tip_message"
    android:textSize="30sp"
    android:text="首页"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

自定义Fragment代码(这里我就给出一个,其他的完全一样,只是修改了Fragment布局中的文本内容):
FirstPageFragment.java代码:

package com.hfut.enmulatetaobaonavbar.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hfut.enmulatetaobaonavbar.R;

/**
 * author:why
 * created on: 2018/10/3 12:11
 * description:
 */
public class FirstPageFragment extends Fragment {

  TextView message;
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout, container, false);
    message=view.findViewById(R.id.tip_message);
    message.setText("这是首页");
    return view;
  }
}

上面就是几个主要的组成部分了,其他的素材就不介绍了,还有就是很多代码可以优化的地方也没有做在太多考虑,下面就来说一说几个需要注意的点:

  • Fragment,FragmentManager,FragmentTransaction的配合使用
  • 淘宝图标与首页菜单项的切换
  • Fragment的包不要使用错了,不是android.app.Fragment
  • 填充FramLayout的时候,注意FragmentTransaction里面的内容的添加与删除
  • 实现的本质其实就是点击事件与FramLayout配合Fragment实现的

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android实现顶部导航栏可点击可滑动效果(仿微信仿豆瓣网)

    使用ViewPager,PagerSlidingTabStrip,SwipeRefreshLayout打造一款可以点击可以侧滑的顶部导航栏. 先简单介绍一下所用的两个个开源库. PagerSlidingTabStrip Github地址 用法: 1.向app Module中的build.gradle中添加依赖 dependencies { compile 'com.astuetz:pagerslidingtabstrip:1.0.1' } 2.把PagerSlidingTabStrip这个控件添

  • Android程序开发之Fragment实现底部导航栏实例代码

    流行的应用的导航一般分为两种,一种是底部导航,一种是侧边栏. 说明 IDE:AS,Android studio; 模拟器:genymotion; 实现的效果,见下图. 具体实现 为了讲明白这个实现过程,我们贴出来的代码多一写,这样更方便理解 [最后还会放出完整的代码实现] .看上图的界面做的比较粗糙,但实现过程的骨架都具有了,想要更完美的设计,之后自行完善吧 ^0^. 布局 通过观察上述效果图,发现任意一个选项页面都有三部分组成: 顶部去除ActionBar后的标题栏: 中间一个Fragment

  • Android仿网易客户端顶部导航栏效果

    最近刚写了一个网易客户端首页导航条的动画效果,现在分享出来给大家学习学习.我说一下这个效果的核心原理.下面是效果图: 首先是布局,这个布局是我从网易客户端反编译后弄来的.大家看后应该明白,布局文件如下: <FrameLayout android:id="@id/column_navi" android:layout_width="fill_parent" android:layout_height="wrap_content" androi

  • Android实现沉浸式通知栏通知栏背景颜色跟随app导航栏背景颜色而改变

    最近好多app都已经满足了沉浸式通知栏, 所谓沉浸式通知栏:就是把用来导航的各种界面操作空间隐藏在以程序内容为主的情景中,通过相对"隐形"的界面来达到把用户可视范围最大化地用到内容本身上. 而最新安卓4.4系统的通知栏沉浸模式就是在软件打开的时候通知栏和软件顶部颜色融为一体,这样不仅可以使软件和系统本身更加融为一体. 就是手机的通知栏的颜色不再是白色.黑色简单的两种了,本人用的小米4手机,米4手机中的自带软件都支持沉浸式通知栏, 举个例子:大家可以看一下自己的qq,它的标题的背景颜色是

  • Android中TabLayout+ViewPager 简单实现app底部Tab导航栏

    前言 在谷歌发布Android Design Support Library之前,app底部tab布局的实现方法就有很多种,其中有RadioGroup+FrameLayout.TabHost+Fragment.FragmentPagerAdapter+ViewPager等方法,虽然这些方法虽然能达到同样的效果,但我个人总觉得有些繁琐.然而,Google在2015的IO大会上,给开发者们带来了全新的Android Design Support Library,里面包含了许多新控件,这些新控件有许多

  • Android实现底部导航栏功能(选项卡)

    现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其中各个类的作用以及资源文件就不详细解释了,还有资源图片(在该Demo中借用了其它应用程序的资源图片)也不提供了,大家可以自行更换自己需要的资源图片.直接上各个布局文件或各个类的代码: 1. res/layout目录下的 maintabs.xml 源码: <?xml version="1.0&q

  • Android项目实战之仿网易顶部导航栏效果

    随着时间的推移现在的软件要求显示的内容越来越多,所以要在小的屏幕上能够更好的显示更多的内容,首先我们会想到底部菜单栏,但是有时候想网易新闻要显示的内容太多,而且又想在主页面全部显示出来,所以有加了顶部导航栏,但是Android这样的移动设备内存是受限的,那么多界面缓存到内存中,很容易导致内存溢出,这个是比较致命的,所以不得不考虑.虽然我在之前也做过网易的顶部导航栏但是方式并不好,就像使用viewpager做一些复杂的界面由于图片占用内存过多,很容易导致内存溢出,学习了今天的内容大家做一下对比相信

  • 超简单的几行代码搞定Android底部导航栏功能

    超简单,几行代码搞定Android底部导航栏-–应项目需求以及小伙伴的留言,新加了两个方法: 设置底部导航栏背景图片 添加底部导航栏选项卡切换监听事件 底部导航栏的实现也不难,就是下边是几个Tab切换,上边一般是一个FrameLayout,然后FrameLayout中切换fragment. 网上有不少关于Android底部导航栏的文章,不过好像都只是关于下边Tab切的,没有实现Tab与fragment的联动,用的时候还要自己手写这部分代码,对我这个比较懒(据说,懒是程序员的一种美德_#)得程序员

  • Android 沉浸式状态栏与隐藏导航栏实例详解

    1 前言 一般我们在Android的APP开发中,APP的界面如下: 可以看到,有状态栏.ActionBar(ToolBar).导航栏等,一般来说,APP实现沉浸式有三种需求:沉浸式状态栏,隐藏导航栏,APP全屏 沉浸式状态栏是指状态栏与ActionBar颜色相匹配, 隐藏导航栏不用多说,就是将导航栏隐藏,去掉下面的黑条. APP全屏是指将状态栏与导航栏都隐藏,例如很多游戏界面,都是APP全屏. 所以,在做这一步时,关键要问清楚产品狗的需求,免得白费功夫. 下面,分别来介绍这三种方式的实现. 2

  • Android实现淘宝底部图标导航栏

    在上一篇中,简单的使用透明主题的Activity实现了仿微信右侧顶部的对话框,上午又花了两个小时研究了一下淘宝底部的导航栏实现,网上的做法也有很多,今天我就使用一种通过基本控件加上布局的方式,没有任何的自定义风格,控件等来实现,还是老样子,先看一下效果图: 下面就来具体看一看如何实现的,还是按照步骤来吧: 绘制主界面 activity_layout.xml代码如下: <?xml version="1.0" encoding="utf-8"?> <R

  • android 全屏去掉底部虚拟导航栏的方法

    如下所示: @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATIO

  • Android 仿淘宝、京东商品详情页向上拖动查看图文详情控件DEMO详解

    一.淘宝商品详情页效果 我们的效果 二.实现思路 使用两个scrollView,两个scrollView 竖直排列,通过自定义viewGroup来控制两个scrollView的竖直排列,以及滑动事件的处理.如下图 三.具体实现 1.继承viewGroup自定义布局View 重写onMeasure()和onLayout方法,在onLayout方法中完成对两个子ScrollView的竖直排列布局,代码如下: 布局文件: <RelativeLayout xmlns:android="http:/

  • Flutter沉浸式状态栏/AppBar导航栏/仿咸鱼底部凸起导航栏效果

    如下图:状态栏是指android手机顶部显示手机状态信息的位置. android 自4.4开始新加入透明状态栏功能,状态栏可以自定义颜色背景,使titleBar能够和状态栏融为一体,增加沉浸感. 如上图Flutter状态栏默认为黑色半透明,那么如何去掉这个状态栏的黑色半透明背景色,让其和标题栏颜色一致,通栏沉浸式,实现如下图效果呢?且继续看下文讲述. 在flutter项目目录下找到android主入口页面MainActivity.kt或MainActivity.java,判断一下版本号然后将状态

  • Android实现淘宝选中商品尺寸的按钮组实例

    话不多说,先上个效果图: 现在我们就来说说里面的一些原理把! 一.原理: 1.其实这里我们用到的是一个ViewGroup控件组,把这些按钮加进去就有这种效果了!不过这里要继承ViewGroup(命名为:GoodsViewGroup)重写里面的一些方法. 2.主要的方法有: GoodsViewGroup按钮组的控件大小 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 里面的按钮每个的位置坐标 protect

  • Android仿淘宝详情页面viewPager滑动到最后一张图片跳转的功能

    需要做一个仿淘宝客户端ViewPager滑动到最后一页,再拖动的时候跳到详情的功能,刚开始没什么思路,后来搜了一下,发现有好几种实现方法,最好的一种就是在ViewPager图片的后面再加一个view,然后滑动viewpager的时候,判断一下就行了. 附一个链接,我写的代码就是参考的这个,稍微改了一点点,先看看效果图. 实现起来比较简单,先写一个滑动加载详情的布局,然后在viewpager的instantiateItem里面判断一下,如果是最后一张,就显示加载详情的那个布局.不过需要注意的是,v

  • Android仿淘宝view滑动至屏幕顶部会一直停留在顶部的位置

    在刚刚完成的项目中,在一个页面中,用户体验师提出引用户操作的入住按钮要一直保留在页面当中,不管页面能滚动多长都得停留在页面的可视区域.最终实现效果如下图所示:   如图中的红色框中的view始终会停留在页面中,如果滑动至页面的顶部,会一直保留在顶部. 下面来说下具体的实现思路: 思路:其实整个页面当中一共有两个视觉效果一样的View,通过滑动的位置来进行View的隐藏和显示来达到这种效果.整个页面的在上下滑动的过程中可以总结为两个状态,状态A(如图1所示),view2在可视区域内时,view1不

  • Android仿淘宝头条基于TextView实现上下滚动通知效果

    最近有个项目需要实现通知栏的上下滚动效果,仿淘宝头条的那种. 我从网上看了一些代码,把完整的效果做了出来.如图所示: 具体代码片段如下: 1.在res文件夹下新建anmin文件夹,在这个文件夹里创建两个文件 (1).anim_marquee_in.xml进入时动画 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/ap

  • Android 仿淘宝商品属性标签页

    需求 1.动态加载属性,如尺码,颜色,款式等 由于每件商品的属性是不确定的,有的商品的属性是颜色和尺码,有的是口味,有的是大小,所以这些属性不能直接写死到页面上. 2.动态加载属性下的标签 每个属性下的标签个数也不是一定的,比如有的商品的尺码是是S,M,XL,有的是均码,也就是每种属性的具体的内容是不一定的. 技术点 自定义ViewGroup,使其中的TextView可以依据内容长短自动换行,如下图所示 实现 布局 通过ListView来显示商品所有属性,每种属性作为ListView的Item.

随机推荐