【发布时间】: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