【问题标题】:How to set Right Toggle Button in Action Bar?如何在操作栏中设置右切换按钮?
【发布时间】:2014-01-16 14:42:14
【问题描述】:

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

对于左侧,我使用左上角的操作栏切换按钮,如下图所示。我可以把那个切换按钮也放在右边吗?

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

我的问题是,android 是否提供像左一样的右切换按钮?

【问题讨论】:

    标签: android


    【解决方案1】:

    您必须对 .xml 文件和 Java 代码进行一些更改。

    第一步

    让我们从您的 .xml 文件开始。

    代码

    首先FrameLayout:代表操作栏,注意我们有ImageView 称为drawer_indicator,这是我们的正确切换。

    第二个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();
                    }
                });
        }
    

    我希望你清楚。

    【讨论】:

      猜你喜欢
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多