Android巧用ActionBar实现下拉式导航
2019/7/7 19:46:23
本文主要是介绍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实现下拉式导航的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路