我剛剛在最近包含在支持庫中的新的DrawerLayout玩弄。我已經實現了一個簡單的示例與多個抽屜,而我使用START和END作爲gravities它的所有工作就像一個魅力,但當試圖添加一個抽屜的頂部或底部gravities它崩潰。使用頂部和底部重力時,Android DrawerLayout失敗
是否可以使用它從頂部和/或底部滑動抽屜?
下面是我的活動和工作狀態下的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);
是當我ge t出現以下錯誤:
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>
我問@羅馬nurik這個在他的[G + (https://plus.google.com/113735310430199015092/posts/bjiNbjAFsT2)關於DrawerLayout的介紹,他指出:_「不,絕對不打算與垂直重力一起使用」_所以我想這沒有意義試圖讓這個工作。任何替代實施垂直抽屜? – luixal
mabe你可以使用SlidingDrawer? –
看看https://github.com/Ali-Rezaei/SlidingDrawer,它可以通過幾行代碼從任何一側滑動。 – Ali