DrawerLayout无法运行并阻止不相关的小部件

时间:2016-05-05 13:26:33

标签: android android-layout textview android-framelayout drawerlayout

我有一个Activity TextView,我想为Activity添加导航抽屉。所以我更改了XML并实施了DrawerLayout

实施DrawerLayout后,它根本无法运行(意味着它无法打开),FrameLayout内实施的DrawerLayoutDrawerLayout无关TextView文字。

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

TextView内的LinearLayout是无法看到的不相关的DrawerLayout小部件。另一个TextView是导航抽屉TextView。我知道导航抽屉小部件必须具有android:layout_gravity="start"属性,但它显然没有布局父级,所以AS不允许我写它。

运行Activity时,我会看到导航栏TextView的文本“立即尝试”,这就是全部。

DrawerActivity.java

public class LyricsActivity扩展了AppCompatActivity {     私有TextView文本;

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);
}

我阅读了所有文档和大量SO帖子,没有任何帮助我,也没有解决这类问题。

1 个答案:

答案 0 :(得分:2)

DrawerLayout根据View属性确定哪些layout_gravity用作抽屉。只需使用适当的值将此属性添加到TextView即可。例如:

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