如何在Action Bar中设置右切换按钮?

时间:2014-01-16 14:42:15

标签: android

我有一个动作栏。它有两个左右抽屉。

对于左侧,我使用左上角的Action Bar Toggle按钮,如下图所示。我也可以在右侧放置切换按钮吗?

我可以使用自定义线性布局视图(ImageView - EditText - ImageView)将其添加到getActionBar.setCustomView()方法,并在imageview上实现单击侦听器以显示抽屉。

我的问题是,android是否提供左侧的右侧切换按钮? enter image description here

1 个答案:

答案 0 :(得分:0)

您已在xml文件中进行了一些更改,java代码

  

第一步

让我们为你的xml文件加注星标

解释代码

第一帧FrameLayout:代表动作栏,注意我们将ImageView称为drawer_indicator,这是我们的权利切换。

Second FrameLayout:表示我们的片段容器

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <!-- Action-bar looking view -->

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/actionbar_dimen"
            android:background="@color/blue" >

            <ImageView
                android:id="@+id/drawer_indicator"
                android:layout_width="@dimen/actionbar_dimen"
                android:layout_height="@dimen/actionbar_dimen"
                android:layout_gravity="right"
                android:background="@drawable/drawer_selector"
                android:scaleType="centerInside" />

            <TextView
                android:id="@+id/indicator_style"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/actionbar_dimen"
                android:layout_gravity="center"
                android:background="@drawable/drawer_selector"
                android:gravity="center"
                android:paddingLeft="12dp"
                android:paddingRight="12dp"
                android:text="@string/tests"
                android:textColor="@android:color/white"
                android:textStyle="bold" />


        </FrameLayout>
  <!-- Action-bar Ends -->



    <!-- Drawer Layout -->

        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

         <!-- our Fragment Content-->

            <FrameLayout
                android:id="@+id/view_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/light_gray"
                android:gravity="center" />
    <!-- slide menu  android:layout_gravity="end" to make it come from right-->
            <LinearLayout
                android:id="@+id/drawer_content"
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:layout_gravity="end"    
                android:background="@android:color/white"
                android:gravity="center"
                android:orientation="vertical" >


             <!-- drawer list-->
                <ListView
                    android:id="@+id/DrawerList"
                    android:layout_width="240dp"
                    android:layout_height="match_parent"
                     />
            </LinearLayout>
        </android.support.v4.widget.DrawerLayout>

    </LinearLayout>
  

第二步

从此链接获取DrawerArrowDrawable

private DrawerArrowDrawable drawerArrowDrawable;
    private float offset;
    private boolean flipped;
    ListView DrawerList;
    DrawerLayout drawer;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // to make activity full screen 
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
         // it must before setContentView
            setContentView(R.layout.home_view);

            // drawer list
            DrawerList = (ListView) findViewById(R.id.DrawerList);

           // drawer item list
            ArrayList<DrawerItem> Items = new ArrayList<DrawerItem>();
            // fill arraylist 
            Items.add(new DrawerItem(getResources().getString(R.string.tests),
                    R.drawable.test));
            // drawer adapter 
            DrawerAdapter adapter = new DrawerAdapter(this, Items);
     // set adapter to our drawer list
            DrawerList.setAdapter(adapter);

             // attach listener to drawer list 
            DrawerList.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {

                    // handle select item here
                }
            });
             // initialize our drawer layout 
            drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
           // right toggle button 
            final ImageView imageView = (ImageView) findViewById(R.id.drawer_indicator);
            final Resources resources = getResources();
           // its custom class 
            drawerArrowDrawable = new DrawerArrowDrawable(resources);
            drawerArrowDrawable.setStrokeColor(resources
                    .getColor(R.color.light_gray));
            imageView.setImageDrawable(drawerArrowDrawable);

            drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
                @Override
                public void onDrawerSlide(View drawerView, float slideOffset) {
                    offset = slideOffset;

                    // Sometimes slideOffset ends up so close to but not quite 1 or 0.
                    if (slideOffset >= .995) {
                        flipped = true;
                        drawerArrowDrawable.setFlip(flipped);
                    } else if (slideOffset <= .005) {
                        flipped = false;
                        drawerArrowDrawable.setFlip(flipped);
                    }

                    drawerArrowDrawable.setParameter(offset);
                }
            });


    // attach listener to toggle image view 
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    openDrawer();
                }
            });
    }

我希望你能清楚