在写安卓的时候,遇到这个问题
我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tl_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/dl_left"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<!--侧滑菜单-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_gravity="start">
<ListView
android:id="@+id/lv_left_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:text="DrawerLayout" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
遇到如下问题
并且闪退
于是去网上搜索解决问题的方案
都是
(1)DrawerLayout里的width和height属性要match_parent
(2)还有在DrawerLayout里的Linearlayout要注意在要在父容器中靠右要用layout_gravity属性而不是gravity
fine,跟着去检查自己的layout文件 but,没有哪里不对啊,但是将DrawerLayout里的width和height属性的值改为固定值如240dp的时候便能正常运行 ,但是如果这样那怎么适应不同机型?
扫描二维码关注公众号,回复:
3023606 查看本文章
ok,继续找bug...
最后 ,无奈 将最外层的Linearlayout修改成为relativeLayout之后再也不报错不闪退了
我也不知道为什么改成relativeLayout后就好了。。。