Android巧用ActionBar实现下拉式导航

 更新时间:2016年05月25日 14:39:23   作者:summerpxy  
这篇文章主要为大家详细介绍了Android巧用ActionBar实现下拉式导航的相关资料,具有一定的实用性和参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了ActionBar下拉式导航的实现代码,供大家参考,具体内容如下

利用Actionbar同样可以很轻松的实现下拉式的导航方式,若想实现这种效果:

1)actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST) 

2)setListNavigationCallbacks(SpinnerAdapter adapter,ActionBar.OnNavigationListener callback).

首先是创建一个Fragment类:

package ccom.app.main;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MyFragment extends Fragment {

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {

  Context context = this.getActivity();

  TextView tv = new TextView(context);

  Bundle arc = this.getArguments();

  int tabs=arc.getInt("key");
  
  tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT));

  tv.setText("hello actionbar "+tabs);

  return tv;

 }

}

main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".Main" >

 <LinearLayout
  android:id="@+id/content"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" />

</RelativeLayout>

自定义的用于显示textview的mytextview.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/text1"
 android:textColor="#fff"
 android:background="#696969"
 android:layout_width="60sp"
 android:layout_height="match_parent"
 android:textAppearance="?android:attr/textAppearanceListItemSmall"
 android:gravity="center_vertical"
 android:paddingStart="?android:attr/listPreferredItemPaddingStart"
 android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>

Main.java

package ccom.app.main;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.ArrayAdapter;

@SuppressLint("NewApi")
public class Main extends Activity implements ActionBar.OnNavigationListener {

 ActionBar actionBar = null;

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

  actionBar = this.getActionBar();

  actionBar.setDisplayShowTitleEnabled(true);

  actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

  actionBar.setListNavigationCallbacks(new ArrayAdapter(Main.this,
    R.layout.mytextview, R.id.text1, new String[] { "tab1", "tab2",
      "tab3" }), this);

 }

 @Override
 public boolean onNavigationItemSelected(int itemPosition, long itemId) {

  MyFragment mf = new MyFragment();
  Bundle bundle = new Bundle();
  bundle.putInt("key", itemPosition + 1);
  mf.setArguments(bundle);

  FragmentTransaction action = this.getFragmentManager()
    .beginTransaction();
  action.replace(R.id.content, mf);
  action.commit();
  return true;
 }

}

实现的效果如图:

以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Android巧用ActionBar实现下拉式导航

    Android巧用ActionBar实现下拉式导航

    这篇文章主要为大家详细介绍了Android巧用ActionBar实现下拉式导航的相关资料,具有一定的实用性和参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • iBeacon使用蓝牙连接范围精确到1-3米

    iBeacon使用蓝牙连接范围精确到1-3米

    这篇文章主要为大家详细介绍了iBeacon使用蓝牙连接范围精确到1到3米,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • Android绘制机器人小实例

    Android绘制机器人小实例

    这篇文章主要为大家详细介绍了Android绘制机器人小实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • 使用Android Studio 开发自己的SDK教程

    使用Android Studio 开发自己的SDK教程

    很多时候我们要将自己开发一个类库打包成jar包以供他调用,这个jar包也叫你自己的SDK或者叫library。android studio生成jar包的方法与eclipse有所不同。在studio中library其实是module的概念。
    2017-10-10
  • Android自定义照相机详解

    Android自定义照相机详解

    几乎每个APP都会用的相机功能,下面小编把内容整理分享到脚本之家平台,供大家参考
    2016-04-04
  • Android_UI 仿QQ侧滑菜单效果的实现

    Android_UI 仿QQ侧滑菜单效果的实现

    相信大家对QQ侧滑菜单的效果已经不陌生了吧,侧滑进入个人头像一侧,进行对头像的更改,我的收藏,QQ钱包,我的文件等一系列的操作,下面小编给大家分享Android_UI 仿QQ侧滑菜单效果的实现,一起看看吧
    2017-04-04
  • Android模拟实现华为系统升级进度条

    Android模拟实现华为系统升级进度条

    这篇文章主要介绍了如何通过Android模拟实现华为在系统升级时显示的进度条。文中的实现过程讲解详细,感兴趣的小伙伴可以动手试一试
    2022-01-01
  • Android 列表倒计时的实现的示例代码(CountDownTimer)

    Android 列表倒计时的实现的示例代码(CountDownTimer)

    本篇文章主要介绍了Android 列表倒计时的实现的示例代码(CountDownTimer),具有一定的参考价值,有兴趣的可以了解一下
    2017-09-09
  • Android基于Service的音乐播放器

    Android基于Service的音乐播放器

    这篇文章主要为大家详细介绍了Android基于Service的音乐播放器,本文开发一个基于Service的音乐播放器,音乐由后台运行的Service负责播放,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android ViewDragHelper使用方法详解

    Android ViewDragHelper使用方法详解

    这篇文章主要为大家详细介绍了Android ViewDragHelper的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12

最新评论