【问题标题】:Button beneath navigation drawer not working when pressed导航抽屉下方的按钮在按下时不起作用
【发布时间】:2015-07-02 10:10:47
【问题描述】:

然而,当按钮被放置在层次结构中以便可以在导航抽屉的顶部看到时,按钮可以正常工作。但是,当按钮滑出时,它应该隐藏在导航抽屉后面,所以这是不可取的。

以下是 MainActivity.java

中的代码
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks {

public ProgressDialog progBar;

public final static boolean DEBUG = false;
public final static String TAG = "AppGetter";
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;



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

    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false); 

    mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);

    ImageButton cart_button = (ImageButton) findViewById(R.id.button2);
    cart_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            start_request();

        }
    });



}

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

@Override
public void onNavigationDrawerItemSelected(int position) {
    Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
}

@Override
public void onBackPressed() {
    if (mNavigationDrawerFragment.isDrawerOpen())
        mNavigationDrawerFragment.closeDrawer();
    else
        super.onBackPressed();
}


public void start_request()
{
    String pkg = getPackageName();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName(pkg,pkg+".RequestActivity"));
    startActivity(intent);

    if(DEBUG)Log.v(TAG,"Intent intent: "+intent);
}
}

我假设问题出在上面的类中,但为了完整起见,我在下面粘贴了两个感兴趣的 XML 文件。

activity_main.xml 如您所见,ImageButton 目前在层次结构中位于导航抽屉的“上方”,以使其被导航抽屉覆盖。将 ImageButton 移到“下方”可以使按钮正常工作,但会导致它出现在导航抽屉的顶部(并且不像布局的其余部分那样着色)。

<FrameLayout
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">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fontify="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="@color/myPrimaryColor">

    <View
        android:id="@+id/block1"
        android:layout_height="240dp"
        android:layout_width="match_parent"
        android:layout_below="@+id/toolbar_actionbar"
        android:background="@drawable/block_primary"
        />

    <TextView
        android:id="@+id/title1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/toolbar_actionbar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="@string/title"
        android:textColor="@color/white"
        android:textSize="34sp"
        android:fontFamily="sans-serif"
        />

    <include
        android:id="@+id/toolbar_actionbar"
        layout="@layout/toolbar_default"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />

    <ImageButton
        android:id="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="268dp"
        android:src="@drawable/ic_chevron_up"
        android:background="@drawable/fab_simple"/>

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

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

        <fragment
            android:id="@+id/fragment_drawer"
            android:name="com.onepersonco.iconrequestbase.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer" />
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

fragment_navigation_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<!-- Provides a margin at the top of the navigation drawer. -->
<View
    android:id="@+id/navWhiteSpace1"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="@color/myNavigationDrawerBackgroundColor"
    />


<android.support.v7.widget.RecyclerView
android:id="@+id/drawerList"
android:layout_below="@+id/navWhiteSpace1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:scrollbars="vertical"
android:scrollbarDefaultDelayBeforeFade="0"
android:scrollbarFadeDuration="0"
android:overScrollMode="never"
android:focusable="true"
android:background="@color/myNavigationDrawerBackgroundColor"/>

</RelativeLayout>

【问题讨论】:

  • 可以贴出你正在使用的xml布局吗?

标签: android android-intent onclicklistener navigation-drawer


【解决方案1】:

我在尝试在线学习各种导航抽屉教程时遇到了同样的问题。

对我有用的是在 GitHub 上使用 Mike Penz 的 MaterialDrawer 库。他在页面底部的“readme”文件中有一个非常简单的教程。

希望其他对 Java 有更好理解的人可以解释您的代码失败的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 2020-06-04
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多