android - NavigationDrawer布局元素不可点击

标签 android android-layout android-fragments android-view

我使用了 android 教程中的确切示例:http://developer.android.com/training/implementing-navigation/nav-drawer.html

构建抽屉导航。在此 Activity 中,我希望允许一个可单击的按钮,我有它的代码。但是当我去点击按钮时,没有任何反应。我不明白为什么这不起作用。

我曾尝试在 Activity 之上膨胀另一个 View ,但没有成功,而且我不太确定还可以尝试做些什么来让我的按钮可点击!

我的抽屉导航工作得很好,只是按钮不可点击是有问题的。因此,给出的代码是最少的:

极简activity(HomeActivity.java)

public class HomeActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * The order must be maintained:
         * 1) setContext
         * 2) initActivityManager
         * 3-N) anything else
         */
        Singletons.ContextManager.setContext(getApplicationContext());
        Singletons.AppManager.initActivityManager();
        // This has to happen before the services start
        Singletons.Coordinator.setScreenState(true);
        // Inflate our layout
        setContentView(R.layout.activity_app_drawer);

        // Its always useful to keep this saved somewhere
        this.appContext = this;

        // Initialize our drawer
        this.title = "Home";
        // These two must always align
        this.fragmentTitles = new String[]{"Home", "View App", "View History", "View Location", "Manage Services"};
        this.fragmentClasses = new String[]{
                "edu.ucr.aum.fragments.HomeFragment",
                "edu.ucr.aum.fragments.AppStatsFragment",
                "edu.ucr.aum.fragments.BrowserStatsFragment",
                "edu.ucr.aum.fragments.LocationFragment",
                "edu.ucr.aum.fragments.ServicesFragment"};
        this.drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        this.drawerList = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for our drawer
        this.drawerList.setAdapter(
                new ArrayAdapter<String>(
                        this,
                        R.layout.ad_text_view,
                        this.fragmentTitles
                )
        );
        // Set the click listener
        this.drawerList.setOnItemClickListener(new DrawerItemClickListener());

        this.drawerToggle = new ActionBarDrawerToggle(
                this,
                this.drawerLayout,
                R.drawable.ic_drawer,
                R.string.drawer_open,
                R.string.drawer_close
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(title);
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(title);
            }
        };

        this.drawerLayout.setDrawerListener(this.drawerToggle);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        /** Section for my button **/
        Button exitButton = (Button) findViewById(R.id.btnStartExitSurvey);
        long installDate = this.preferences.getLong("INSTALL_TIME", System.currentTimeMillis());

        TextView tv = (TextView) findViewById(R.id.tvExitSurveyInstructions);
        tv.setText(Html.fromHtml(getString(R.string.homeInstructions)));
        if(Singletons.Coordinator.displayExitSurvey(installDate)) {
            exitButton.setVisibility(View.VISIBLE);
            exitButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(appContext, SurveyActivity.class);
                    startActivity(i);
                }
            });
        } else {
            exitButton.setVisibility(View.INVISIBLE);
        }
    }
}

我的 xml 文件(activity_app_drawer.xml):

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="fill_parent"
    android:layout_width="wrap_content">
<TextView
    android:id="@+id/tvExitSurveyInstructions"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/homeInstructions"
    />

    <Button
        android:id="@+id/btnStartExitSurvey"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/homeButtonStartExitSurvey"
        android:layout_below="@+id/tvExitSurveyInstructions"
        />

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <!-- 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:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

</RelativeLayout>

最佳答案

您的布局似乎有误。 DrawerLayout 应该是父级和内容 2 或 3 个子级:

  • 左边的抽屉导航;
  • 右边的抽屉导航;
  • 一个可以包含子元素的容器 - 您布局中的 FrameLayout

所以,你的布局应该是这样的:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
>
    <!-- The main content view -->
    <RelativeLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
    >
        <TextView
            android:id="@+id/tvExitSurveyInstructions"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/homeInstructions"
        />

        <Button
            android:id="@+id/btnStartExitSurvey"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/homeButtonStartExitSurvey"
            android:layout_below="@+id/tvExitSurveyInstructions"
        />
    </RelativeLayout>

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

关于android - NavigationDrawer布局元素不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24089078/

相关文章:

android - 过滤加速度计数据噪声

android - 将数据从服务传递到 Activity

Android:通过Activity中的LiveData和ViewModel观察Room DB

java - Android 动态标题栏

android - 无法从 Android 10 Web View 访问相机

java - 如何以编程方式调用 Android 布局 (XML)?

android - Material 设计 - 操作栏标题和内容的左边距不匹配

android - 在通过 fragment 导航时正确处理 DrawerLayout 动画

android - 下拉抽屉布局 Android

java - Android:保持fragment在后台运行