【问题标题】:DrawerLayout not functioning and blocks not related widgetsDrawerLayout 不起作用并阻止不相关的小部件
【发布时间】:2016-05-05 13:26:32
【问题描述】:

我有一个Activity,它有一个TextView,我想在Activity 上添加一个导航抽屉。所以我改变了我的XML并实现了DrawerLayout

在实现DrawerLayout 之后,它根本无法运行(意味着它不会打开),并且在DrawerLayout 中实现的FrameLayout 会阻止与DrawerLayout 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>
-->

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

运行Activity 时,我看到导航抽屉TextView 的文本“立即尝试”,仅此而已。

DrawerActivity.java

公共类 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 帖子,但没有任何帮助或解决此类问题。

【问题讨论】:

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


    【解决方案1】:

    DrawerLayout 通过layout_gravity 属性确定将哪个Views 用作抽屉。只需使用适当的值将此属性添加到您的TextView。例如:

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

    【讨论】:

    • 我写了我不能。 XML 文件不知何故无法将 TextView 识别为 DrawerLayout 的子视图。但感谢您的回答。还有其他想法吗?
    • 你是什么意思?你有错误吗?布局的其余部分看起来不错。
    • 只是没有提示我开始写android:layout_g。并且所有其他属性都会提示。
    • 你的布局很好。如果您的 IDE 出现问题,请尝试清理并重建项目。另外,我不知道您使用的是哪个 IDE,但在我的 IDE 中,我没有输入 android: 前缀以使其自动完成;只是属性名称,例如layout_g...
    • 作为。我刚刚添加了android:layout_gravity="start",抽屉仍然无法正常工作,但Activity 中没有显示文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    相关资源
    最近更新 更多