我有一个Activity
TextView
,我想为Activity
添加导航抽屉。所以我更改了XML
并实施了DrawerLayout
。
实施DrawerLayout
后,它根本无法运行(意味着它无法打开),FrameLayout
内实施的DrawerLayout
与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>
-->
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帖子,没有任何帮助我,也没有解决这类问题。
答案 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"
/>