我只是在玩最近包含在支持库中的新DrawerLayout 。我已经实现了一个带有多个抽屉的简单示例,当我使用 START 和 END 作为重力时,这一切都像一个魅力,但是当尝试添加一个具有 TOP 或 BOTTOM 重力的抽屉时它会崩溃。
是否可以将其用于从顶部和/或底部滑动抽屉?
以下是我的活动的完整代码和处于工作状态的 xml 布局;如果我尝试改变这一点:
<ListView android:id="@+id/right_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#007799"/>
进入这个(注意 layout_width、layout_height 和 layout_gravity 的变化):
<ListView android:id="@+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="bottom"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#007799"/>
和行:
this.drawer.openDrawer(GravityCompat.START);
信息这个:
this.drawer.openDrawer(Gravity.BOTTOM);
是当我收到以下错误时:
05-16 05:22:33.981 1503-1503/es.luixal.test E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{es.luixal.test/es.luixal.test.MainActivity}: java.lang.IllegalArgumentException: View android.widget.RelativeLayout{42664c80 V.E..... ......I. 0,0-0,0} is not a sliding drawer
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: View android.widget.RelativeLayout{42664c80 V.E..... ......I. 0,0-0,0} is not a sliding drawer
at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:970)
at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1003)
at es.luixal.test.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
为什么我不能使用其他重力的任何线索?
谢谢!
MainActivity.java
package es.luixal.test;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// filling list items:
ListView listView = (ListView)findViewById(R.id.left_drawer);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{
"MenuItem 1",
"MenuItem 2",
"MenuItem 3"
});
listView.setAdapter(adapter);
//
this.drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
this.drawer.openDrawer(GravityCompat.START);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onStart() {
super.onStart();
}
}
activity_main.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hi!"
/>
</RelativeLayout>
<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="#44aa00"/>
<ListView android:id="@+id/right_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#007799"/>
</android.support.v4.widget.DrawerLayout>