DrawerLayout不起作用并且阻止了不相关的小部件 [英] DrawerLayout not functioning and blocks not related widgets

查看:120
本文介绍了DrawerLayout不起作用并且阻止了不相关的小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Activity,其中有一个TextView,我想在该Activity中添加一个导航抽屉.因此,我更改了XML并实现了DrawerLayout.

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.

在实现DrawerLayout之后,它根本无法运行(意味着它无法打开),并且在DrawerLayout中实现的FrameLayout阻止了与DrawerLayout TextView文本无关的内容.

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

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

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.

运行Activity时,我看到导航抽屉TextView的文本立即尝试".

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

DrawerActivity.java :

公共类LyricsActivity扩展了AppCompatActivity { 私人TextView文本;

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

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

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

推荐答案

DrawerLayout通过其layout_gravity属性确定将哪些View用作抽屉.只需使用适当的值将此属性添加到您的TextView中即可.例如:

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

这篇关于DrawerLayout不起作用并且阻止了不相关的小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆