通用导航菜单抽屉在Android中

时间:2015-09-30 07:10:54

标签: android navigation-drawer android-framelayout

我需要在我的Android应用程序中添加导航抽屉菜单。我的应用程序有很多模块,所以我不能编码每个活动来显示导航菜单。所以我决定将导航菜单代码放在我的基础活动中。所以每个活动都扩展了这个基础活动导航抽屉菜单正常工作但问题是活动组件无法正常工作。我不知道发生了什么。是否需要进行任何更改?提前谢谢。

baseActivity.java 
public class BaseActivity extends ActionBarActivity {
RelativeLayout fullLayout;
DrawerLayout dLayout;

@Override
public void setContentView(int layoutResID) {
    fullLayout = (RelativeLayout) getLayoutInflater().inflate(
            R.layout.activity_base, null);
    FrameLayout frameLayout = (FrameLayout) fullLayout
            .findViewById(R.id.content_frame);
    ListView dList = (ListView) fullLayout.findViewById(R.id.left_drawer);
    dLayout = (DrawerLayout) fullLayout.findViewById(R.id.drawer_layout);
    getLayoutInflater().inflate(layoutResID, frameLayout, true);
    setContentView(fullLayout);

    String[] menu = new String[] { "Home", "Android", "Windows", "Linux",
            "Raspberry Pi", "WordPress", "Videos", "Contact Us" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, menu);

    dList.setAdapter(adapter);
    dList.setSelector(android.R.color.holo_blue_dark);

    dList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,
                long id) {
            dLayout.closeDrawers();
        }
    });
    }};

activity_base.xml

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="sas.mobi.lakshmi.main.BaseActivity" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

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

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#fff"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

homeActivity.java

public class HomeAndSettingActivity extends BaseActivity {
private Button btnAccount;
private Button btnCollection;
private Button btnOthers;
private Button btnTempExit;
private Button btnExit;
private AdminDS adminDS;
private AdminDO adminDO;
private FinanceDS financeDS;
private PartnerPaymentDS paymentDS;
private GoogleCloudMessaging gcm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_and_setting);
    initializeComponents();
    btnAccount.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),
                        AccountRegHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
        btnCollection.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),
                        CollectionHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
        btnOthers.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),
                        OthersHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
        btnTempExit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnExit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                financeDS = new FinanceDS(getApplicationContext());
                boolean isUps = financeDS.closeActiveCurrentFinances();
                if (isUps) {
                    finish();
                }
            }
        });

}

/**
 * method to initialize components
 */
private void initializeComponents() {
    btnAccount = (Button) findViewById(R.id.btnAccount);
    btnCollection = (Button) findViewById(R.id.btnCollection);
    btnOthers = (Button) findViewById(R.id.btnOthers);
    btnTempExit = (Button) findViewById(R.id.btnTempExit);
    btnExit = (Button) findViewById(R.id.btnExit);
}};

acitivity_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<Button
    android:id="@+id/btnAccount"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:background="?android:attr/dividerVertical"
    android:text="@string/btnAccount"
    android:textSize="14sp" />

<Button
    android:id="@+id/btnCollection"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:background="?android:attr/dividerVertical"
    android:text="@string/btnCollection"
    android:textSize="14sp" />

<Button
    android:id="@+id/btnOthers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:background="?android:attr/dividerVertical"
    android:text="@string/btnOthers"
    android:textSize="14sp" />

<Button
    android:id="@+id/btnTempExit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:background="?android:attr/dividerVertical"
    android:text="@string/btnTempExit"
    android:textSize="14sp" />

<Button
    android:id="@+id/btnExit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:background="?android:attr/dividerVertical"
    android:text="@string/btnExit"
    android:textSize="14sp" />

此家庭活动和常见的导航抽屉菜单显示正常,但家庭活动中的组件无法正常工作。但抽屉组件正在工作。

3 个答案:

答案 0 :(得分:1)

以这种方式更改主xml并将侧边菜单加载到侧边菜单框中:

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <RelativeLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context="sas.mobi.lakshmi.main.BaseActivity" >

         <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>

    <FrameLayout
          android:id="@+id/side_menu"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

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

答案 1 :(得分:1)

你好使用片段来实现导航抽奖使用不能很好地进行活动,因为你需要在菜单的每次通话中更换片段,请找到以下链接http://www.androidhive.info/2015/04/android-getting-started-with-material-design/ 或者您可以通过从新项目中选择nav ui在android studio中创建新项目

答案 2 :(得分:1)

在修改here时,您需要在FrameLayout内移动content_frame DrawerLayout <RelativeLayout xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="sas.mobi.lakshmi.main.BaseActivity" > <android.support.v4.widget.DrawerLayout 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/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#fff" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout>

emailformat='both'