Actionbar实现下拉式导航

利用Actionbar同样可以很轻松的实现下拉式的导航方式,相比与tab的方式下拉式的导航方式更类型于html中的<select>标签。若想实现这种效果:1)actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST)  2)setListNavigationCallbacks(SpinnerAdapter adapter,ActionBar.OnNavigationListener callback).

首先是创建一个Fragment类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
< 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:

?
1
2
3
4
5
6
7
8
9
10
11
12
<? 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 ;
     }
 
}
实现的效果如图:

a1.png

a2.png


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值