我在这里遇到了类似的问题及其解决方案,但在花了几个小时之后,我无法解决我的问题。我查看了许多解决方案,尝试过它们,但仍无法看到我屏幕上的导航抽屉。如果有人能告诉我这里缺少什么,我将非常感激。
提前致谢。
这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="400dp"
android:layout_height="400dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame" >
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Menu">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:text="test"
android:id="@+id/text" />
</RelativeLayout>
<ListView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:background="#fff"
android:id="@+id/left_drawer" >
</ListView>
</android.support.v4.widget.DrawerLayout>
这是我的Menu.java:
>public class Menu extends AppCompatActivity {
>
> public DrawerLayout dlayout;
> public ListView flist;
> public String[] menu;
> //public ArrayAdapter<String> mAdapter;
>
> @Override
> protected void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.activity_menu);
>
> menu = getResources().getStringArray(R.array.nav_drawer_items);
> dlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
> flist = (ListView) findViewById(R.id.left_drawer);
>
> flist.setAdapter(new ArrayAdapter<String>(this, R.layout.activity_menu, >R.id.text, menu));
> }
>}
我没有看到任何输出。Screen output
答案 0 :(得分:2)
似乎布局文件中应该存在问题。
请注意,
DrawerLayout
允许最大 2 子视图。有 查看this Google文档
应该是这样的:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
答案 1 :(得分:0)
您需要切换抽屉的打开/关闭。 为此,您可以执行以下操作:
1 - 实施toggleDrawer方法
public void toggleDrawer(boolean shouldBeVisible){
if(shouldBeVisible){
dlayout.openDrawer(dlayout);
} else {
dlayout.closeDrawers();
}
}
2 - 在onCreate上调用它(注意:这仅用于测试)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
toggleDrawer(View.VISIBLE);
}
3 - 像Daryl说的那样,实施一个按钮,以便您可以主动切换(根据自己的喜好打开和关闭抽屉)
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
if(dlayout.getVisiblity() == View.VISIBLE) {
// drawer is visible -> close
toggleDrawer(false);
} else {
// drawer is gone/invisible -> open
toggleDrawer(true);
}
}
})
4 - (确保您的DrawerLayout仅包含2个子视图)
要添加导航抽屉,请使用a声明您的用户界面 DrawerLayout对象作为布局的根视图。在 - 的里面 DrawerLayout,添加一个包含主要内容的视图 屏幕(隐藏抽屉时的主要布局)和另一个 包含导航抽屉内容的视图。
注意:忘了添加这个,但确实是一个非常宝贵的参考(感谢@Meet,upvoted)
答案 2 :(得分:0)
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/header"
layout="@layout/menu"/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/frame"
android:textSize="24sp"
android:gravity="center"
android:layout_marginTop="50dp"/>
<ListView
android:id="@+id/list_item"
android:layout_width="200dp"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:layout_gravity="left|start"
android:background="#ffeeeeee"/></android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends Activity {
String[] names = {"android","java","spring","html"};
ListView list;
FrameLayout frame;
ActionBarDrawerToggle action;
DrawerLayout drawer;
Button but;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
frame = (FrameLayout) findViewById(R.id.frame);
list = (ListView) findViewById(R.id.list_item);
but = (Button) findViewById(R.id.menu);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, names);
list.setAdapter(adapter);
action = new ActionBarDrawerToggle(this, drawer, R.string.drawer_open, R.string.drawer_close) {
/* Called when drawer is closed */
public void onDrawerClosed(View view) {
//Put your code here
}
/* Called when a drawer is opened */
public void onDrawerOpened(View drawerView) {
//Put your code here
}
};
drawer.setDrawerListener(action);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawer != null) {
if (drawer.isDrawerOpen(list)) {
drawer.closeDrawer(list);
} else {
drawer.openDrawer(list);
process();
}
}
}
});
}
private void process() {
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction ft=getFragmentManager().beginTransaction();
switch (names[position])
{
case "android":
{
yourClass obj=new yourClass();
//ft.replace(R.id.frame,obj);
break
}
case "java":
{
yourClass obj=new yourClass();
//ft.replace(R.id.frame,obj);
break;
}
case "spring":
{
yourClass obj=new yourClass();
//ft.replace(R.id.frame,obj);
break;
}
case "html":
{
yourClass obj=new yourClass();
//ft.replace(R.id.frame,obj);
break;
}
default:
{
Toast.makeText(MainActivity.this,"you have to click another one",Toast.LENGTH_LONG).show();
}
}
ft.commitAllowingStateLoss();
list.setItemChecked(position, true);
drawer.closeDrawer(list);
}
});
}
}
menu.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
tools:context=".MainActivity"
android:layout_height="wrap_content">
<Button
android:id="@+id/menu"
android:title="menu"
android:icon="@drawable/menu"
android:background="@drawable/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#0a0a0a" /></LinearLayout>