android - Android 中常见的导航菜单抽屉

标签 android navigation-drawer android-framelayout

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

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

此主页 Activity 和通用抽屉导航菜单显示正常,但主页 Activity 内的组件无法工作。但抽屉组件正在工作。

最佳答案

将主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>

关于android - Android 中常见的导航菜单抽屉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32860003/

相关文章:

android - 如何使用 Gradle 脚本为应用程序构建 android 包?

java - Java Realm 上有 "and"子句?

android - 抽屉布局打开时主要布局内容仍然可见

android - 从相机获取图像时布局 getHeight() 和 getWidth() 返回 0

android - 是否可以在 onLayout 事件中将 View 添加到布局?

android - 如何从 Kotlin 代码动态设置权重属性?

android - 在 Android Studio 中解锁 APK 文件以进行编辑

java - 抽屉导航 Activity ,通过 fragment 发送

java - Fragment 中的 RecyclerView – 如何从数组列表中检索数据

android - 在 android 中以编程方式创建布局 - 问题