右抽屉不会以双抽屉布局以编程方式关闭

时间:2015-04-03 19:17:51

标签: android android-layout android-support-library android-navigation

我的抽屉布局中有两个抽屉(左右)(或开始和结束)。

问题是我似乎无法以编程方式关闭右抽屉..我可以打开或关闭左抽屉但只能打开右抽屉。

我正在做一些黑客让抽屉出现在动作栏上,所以我不确定这是不是问题..

在我的活动中,你会看到我正在呼叫一个" showLanscapeMethod"当设备配置更改为横向..这就是我要确保所有抽屉都关闭的地方..

失败的地方

private void showLandScape(){
            actionBar.hide();
             drawer.closeDrawer(right_drawer);//Doesn't work
             drawer.closeDrawer(left_drawer);//works
             drawer.closeDrawers();// only closes right

整个活动

public class MainActivity extends ActionBarActivity {

    private static int qrdimension;
    private static int screenWidth;
    private static int screenHeight;
    private static ActionBar actionBar;
    private static  DrawerLayout drawer;

    View left_drawer;
    View right_drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);

        // Inflate the "decor.xml"
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        drawer = (DrawerLayout) inflater.inflate(R.layout.decor, null); // "null" is important.

        // HACK: "steal" the first child of decor view
        ViewGroup decor = (ViewGroup) getWindow().getDecorView();
        View child = decor.getChildAt(0);
        decor.removeView(child);
        FrameLayout container = (FrameLayout) drawer.findViewById(R.id.container); // This is the container we defined just now.
        container.addView(child);

        // Make the drawer replace the first child
        decor.addView(drawer);

        left_drawer = (View)findViewById(R.id.left_drawer);
        right_drawer = (View)findViewById(R.id.right_drawer);


    }

    @Override
    protected void onResume() {
        super.onResume();

        //Find screen size
        WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        screenWidth = point.x;
        screenHeight = point.y;

        if(screenWidth<screenHeight){
            showPortrait();
        }else{
            showLandScape();
        }
        drawer.openDrawer(right_drawer);//this works



    }

    // Check screen orientation or screen rotate event here
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);


        Log.v("ROTATION", "orientation:" + newConfig.orientation);

        // Checks the orientation of the screen for landscape and portrait and set portrait mode always
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            showLandScape();

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            showPortrait();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void showLandScape(){
        actionBar.hide();
         drawer.closeDrawer(right_drawer);//Doesn't work
         drawer.closeDrawer(left_drawer);//works
         drawer.closeDrawers();// only closes right
        //Encode with a QR Code image
        QRCodeEncoder qrCodeEncoder = new QRCodeEncoder("elidd1",
                null,

                BarcodeFormat.QR_CODE.toString(),
                screenHeight);
        try {
            Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
            ImageView myImage = (ImageView) findViewById(R.id.qrcode_view);
            myImage.setImageBitmap(bitmap);

        } catch (WriterException e) {
            e.printStackTrace();
        }

    }

    private void showPortrait(){
        actionBar.show();

        ImageView myImage = (ImageView) findViewById(R.id.qrcode_view);

        myImage.setImageResource(R.drawable.ic_rotate);
    }


}

布局

activity_main

<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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/bg_duo_black"
   >

    <ImageView
        android:id="@+id/qrcode_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />



</RelativeLayout>

decor.xml

<android.support.v4.widget.DrawerLayout
    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=".MainActivity"

    >

    <FrameLayout android:id="@+id/container"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>



    <!-- The navigation drawer -->
    <View android:id="@+id/right_drawer"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@color/electric_blue"/>
    <!-- The navigation drawer -->
    <View android:id="@+id/left_drawer"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@color/aluminum_gray"/>

</android.support.v4.widget.DrawerLayout>

0 个答案:

没有答案