java - 屏幕右侧的抽屉?

标签 java android xml

我是 Android 开发新手,想了解一些东西。我正在为我的应用程序使用抽屉导航,但它位于左侧。我想知道是否有办法将其放在右侧以及如何进行。

activity_home.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- The main content view -->

<FrameLayout
    android:id="@+id/content_frame"

    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->

<ListView

    android:id="@+id/drawer_list"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

drawer_list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"

android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

fragment_layout.xml

<?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" >

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我的Home.java

package com.colourity.snatsh;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Home extends Activity {

// Within which the entire activity is enclosed
private DrawerLayout mDrawerLayout;

// ListView represents Navigation Drawer
private ListView mDrawerList;



// Title of the action bar
private String mTitle = "";

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    mTitle = "JAVATECHIG.COM";
    getActionBar().setTitle(mTitle);

    // Getting reference to the DrawerLayout
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerList = (ListView) findViewById(R.id.drawer_list);




    // Creating an ArrayAdapter to add items to the listview mDrawerList
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), 
            R.layout.drawer_list_item, getResources().getStringArray(R.array.menus));

    // Setting the adapter on mDrawerList
    mDrawerList.setAdapter(adapter);

    // Enabling Home button
    getActionBar().setHomeButtonEnabled(true);

    // Enabling Up navigation
    getActionBar().setDisplayHomeAsUpEnabled(true);

    // Setting item click listener for the listview mDrawerList
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // Getting an array of rivers
            String[] menuItems = getResources().getStringArray(R.array.menus);

            // Currently selected river
            mTitle = menuItems[position];

            // Creating a fragment object
            WebViewFragment rFragment = new WebViewFragment();

            // Passing selected item information to fragment
            Bundle data = new Bundle();
            data.putInt("position", position);
            data.putString("url", getUrl(position));
            rFragment.setArguments(data);


            // Getting reference to the FragmentManager
            FragmentManager fragmentManager = getFragmentManager();

            // Creating a fragment transaction
            FragmentTransaction ft = fragmentManager.beginTransaction();

            // Adding a fragment to the fragment transaction
            ft.replace(R.id.content_frame, rFragment);

            // Committing the transaction
            ft.commit();

            // Closing the drawer
            mDrawerLayout.closeDrawer(mDrawerList);

        }
    });
}

protected String getUrl(int position) {
    switch (position) {
    case 0:
        return "http://javatechig.com";
    case 1:
        return "http://javatechig.com/category/android/";
    case 2:
        return "http://javatechig.com/category/blackberry/";
    case 3:
        return "http://javatechig.com/category/j2me/";
    case 4:
        return "http://javatechig.com/category/sencha-touch/";
    case 5:
        return "http://javatechig.com/category/phonegap/";
    case 6:
        return "http://javatechig.com/category/java/";
    default:
        return "http://javatechig.com";
    }
}



/** Called whenever we call invalidateOptionsMenu() */

public boolean onPrepareOptionsMenu(Menu menu) {
    // If the drawer is open, hide action items related to the content view
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}
}

最佳答案

正如文档所述:

http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right. (Or start/end on platform versions that support layout direction.)

所以,是的,您也可以使用 android:layout_gravity 属性将其放在右侧:)

关于java - 屏幕右侧的抽屉?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896301/

相关文章:

android - "realtime"使用 AsyncTask 搜索?

python - 如何从 Google App Engine 应用程序解析 XML?

java - 如何将 TextView 中图像的位置更改为左下侧?

java - XMLSignature默认为RSA,如何验证SSL签名的DSA曲线?

java - 使用Java编写加密-解密程序

java - Vaadin 在填充后设置组合框值

java - 在 MT4J 中运行高级 MTShell 时出错

java - guice 辅助注入(inject)工厂中通用返回类型的问题

Android TextureView.getBitmap() 很慢

android - 如何加密一个字符串,使其可以用作文件路径的一部分,然后在 Android 中解密?