DrawerLayout not functioning and blocks not relate

2020-05-05 16:59发布

问题:

I havd an Activity which has a TextView and i wanted to add a navigation drawer to that Activity. So I changed my XML and implemented DrawerLayout.

After implementing the DrawerLayout it simply does not functioning(meaning it don't open) and the FrameLayout implemented within the DrawerLayout blocks the not related to the DrawerLayout TextView text.

drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.dl.master.lyrics.lyricsmaster.LyricsActivity"
    android:id="@+id/drawer_layout_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- The main content view where fragments are loaded -->
        <FrameLayout
            android:id="@+id/frame_layout_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/text_tv_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    </LinearLayout>


    <!-- The navigation drawer -->
    <TextView
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:text="Try me! NOW"
        />

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

<!--
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>
-->

The TextView inside the LinearLayout is the not related DrawerLayout widgets which cannot be seen. And the other TextView is the navigation drawer TextView. I know the navigation drawer widget has to have android:layout_gravity="start" property but it apparently has no layout parent so AS does not let me write it.

When running the Activity i see the text "Try it NOW" of the navigation drawer TextView and that's all.

DrawerActivity.java:

public class LyricsActivity extends AppCompatActivity { private TextView text;

private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;

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

    this.initializeViews();

    this.actionBarDrawerToggle = new ActionBarDrawerToggle(LyricsActivity.this,this.drawerLayout,
            null,R.string.open_drawer_description,R.string.close_drawer_description);
    this.drawerLayout.addDrawerListener(actionBarDrawerToggle);
    //this.drawerLayout.openDrawer(GravityCompat.START); // java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
}

@Override
protected void onResume()
{
    super.onResume();

    this.text.setText(getIntent().getStringExtra("Tags"));
}

/**
 * Initialize all my views.
 *
 */
public void initializeViews()
{
    this.lyrics = (TextView) findViewById(R.id.text_tv_id);
    this.drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_id);
}

I read all the documentation and a lot of SO post and nothing helped me nor addressed this kind of problem.

回答1:

DrawerLayout determines which Views to use as a drawers by their layout_gravity attribute. Simply add this attribute to your TextView with the appropriate value. For example:

<!-- The navigation drawer -->
<TextView
    android:layout_width="220dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    android:text="Try me! NOW"
/>