利用Actionbar同样可以很轻松的实现下拉式的导航方式,相比与tab的方式下拉式的导航方式更类型于html中的标签。若想实现这种效果:1)actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST) 2)setListNavigationCallbacks(SpinnerAdapter adapter,ActionBar.OnNavigationListener callback). 首先是创建一个Fragment类:
01
packageccom.app.main;
02
03
importandroid.annotation.SuppressLint;
04
importandroid.app.Fragment;
05
importandroid.content.Context;
06
importandroid.os.Bundle;
07
importandroid.view.LayoutInflater;
08
importandroid.view.View;
09
importandroid.view.ViewGroup;
10
importandroid.view.ViewGroup.LayoutParams;
11
importandroid.widget.TextView;
12
13
@SuppressLint("NewApi")
14
publicclassMyFragmentextendsFragment {
15
16
@Override
17
publicView onCreateView(LayoutInflater inflater, ViewGroup container,
18
Bundle savedInstanceState) {
19
20
Context context =this.getActivity();
21
22
TextView tv =newTextView(context);
23
24
Bundle arc =this.getArguments();
25
26
inttabs=arc.getInt("key");
27
28
tv.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,
29
LayoutParams.WRAP_CONTENT));
30
31
tv.setText("hello actionbar "+tabs);
32
33
returntv;
34
35
}
36
37
}
main.xml:
01
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
02
xmlns:tools="http://schemas.android.com/tools"
03
android:layout_width="match_parent"
04
android:layout_height="match_parent"
05
tools:context=".Main">
06
07
<LinearLayout
08
android:id="@+id/content"
09
android:layout_width="match_parent"
10
android:layout_height="match_parent"
11
android:orientation="vertical"/>
12
13
</RelativeLayout>
自定义的用于显示textview的mytextview.xml:
01
<?xmlversion="1.0"encoding="utf-8"?>
02
<TextViewxmlns:android="http://schemas.android.com/apk/res/android"
03
android:id="@+id/text1"
04
android:textColor="#fff"
05
android:background="#696969"
06
android:layout_width="60sp"
07
android:layout_height="match_parent"
08
android:textAppearance="?android:attr/textAppearanceListItemSmall"
09
android:gravity="center_vertical"
10
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
11
android:minHeight="?android:attr/listPreferredItemHeightSmall"
12
/>
Main.java
01
packageccom.app.main;
02
03
importandroid.annotation.SuppressLint;
04
importandroid.app.ActionBar;
05
importandroid.app.Activity;
06
importandroid.app.FragmentTransaction;
07
importandroid.os.Bundle;
08
importandroid.widget.ArrayAdapter;
09
10
@SuppressLint("NewApi")
11
publicclassMainextendsActivityimplementsActionBar.OnNavigationListener {
12
13
ActionBar actionBar =null;
14
15
@Override
16
protectedvoidonCreate(Bundle savedInstanceState) {
17
super.onCreate(savedInstanceState);
18
setContentView(R.layout.main);
19
20
actionBar =this.getActionBar();
21
22
actionBar.setDisplayShowTitleEnabled(true);
23
24
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
25
26
actionBar.setListNavigationCallbacks(newArrayAdapter(Main.this,
27
R.layout.mytextview, R.id.text1,newString[] {"tab1","tab2",
28
"tab3"}),this);
29
30
}
31
32
@Override
33
publicbooleanonNavigationItemSelected(intitemPosition,longitemId) {
34
35
MyFragment mf =newMyFragment();
36
Bundle bundle =newBundle();
37
bundle.putInt("key", itemPosition +1);
38
mf.setArguments(bundle);
39
40
FragmentTransaction action =this.getFragmentManager()
41
.beginTransaction();
42
action.replace(R.id.content, mf);
43
action.commit();
44
returntrue;
45
}
46
47
}
实现的效果如图: