Android巧用ActionBar實現下拉式導航_Android

來源:互聯網
上載者:User

本文執行個體為大家分享了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; }}

實現的效果如圖:

以上就是本文的全部內容,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.