必须使用MeasureSpec.EXACTLY错误测量DrawerLayout

时间:2015-07-31 12:20:55

标签: android navigation-drawer

我正在尝试实现导航抽屉,但我一直收到此错误。 我看到了类似的问题,但对我没有用。我有以下布局activity_main2.xml:

<android.support.v4.widget.DrawerLayout
    android:layout_width="match_parent"
    android:id="@+id/drawerLayout"
    android:layout_height="match_parent">

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

   <RelativeLayout
        android:layout_gravity="left|start"
        android:layout_width="match_parent"
        android:background="#fff"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#eee"
            android:background="#fff"
            android:dividerHeight="1dp" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

在我的activity_main2.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    initView();
    if (toolbar != null) {
        //toolbar.setTitle("");
        setSupportActionBar(toolbar);
    }
    initDrawer();
}

private void initView() {
    leftDrawerList = (ListView) findViewById(R.id.left_drawer);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    navigationDrawerAdapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, leftSliderData);
    leftDrawerList.setAdapter(navigationDrawerAdapter);
    leftDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(position == 1){
                drawerLayout.closeDrawer(Gravity.LEFT);
            }
        }
    });
}

private void initDrawer() {
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
}

希望有人能帮助我找到正确的方向。

非常感谢提前!

12 个答案:

答案 0 :(得分:36)

我的DrawerLayout位于LinearLayout中:

 +(NSFetchedResultsController*)getCommentsForUser:(Users*)user
  {
    NSString* cacheName=@"Root";

    [NSFetchedResultsController deleteCacheWithName:cacheName];
    NSManagedObjectContext* context=[self managedObjectContext];


    NSEntityDescription *entity = [NSEntityDescription  entityForName:NSStringFromClass([self class])   inManagedObjectContext:context];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];
     NSPredicate* predicate=[NSPredicate predicateWithFormat:@"user=%@",user];
    fetchRequest setPredicate:predicate];

     NSFetchedResultsController *theFetchedResultsController =   [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:context   sectionNameKeyPath:nil
                                               cacheName:cacheName];
     return theFetchedResultsController;

}

然后我通过更改<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/abohawawallpapersqr" tools:context=".MainActivity"> <include layout="@layout/toolbar" /> <android.support.v4.widget.DrawerLayout android:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- ...... --> </android.support.v4.widget.DrawerLayout> </LinearLayout> 的{​​{1}}到layout_height="wrap_content"解决了问题。所以代码是:

layout_height="match_parent"

答案 1 :(得分:17)

对于仍然有例外的人,请确保DrawerLayout的身高为match_parent

答案 2 :(得分:14)

由于新ConstraintLayout,也可能发生这种情况。确保抽屉布局不在ConstraintLayout的层次结构中。

这是Android上一个非常愚蠢的错误。我会尝试为它提交一个错误。

答案 3 :(得分:9)

确保您的活动不是&#34;对话框&#34;活动,检查清单。我犯了这个错误并得到了错误。

答案 4 :(得分:7)

如果您将layout_width设置为wrap_content,也会发生这种情况。在我的情况下,它通过将宽度设置为match_parent来解决。希望这有助于某人。

答案 5 :(得分:1)

requestWindowFeature(Window.FEATURE_NO_TITLE);

通过创建如下的活动解决了我的问题;

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);
}

答案 6 :(得分:1)

使用此...

public class CustomDrawerLayout extends DrawerLayout {
public CustomDrawerLayout(Context context) {
       super(context);
}
public CustomDrawerLayout(Context context, AttributeSet attrs) {
       super(context, attrs);
 }
     public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      widthMeasureSpec = MeasureSpec.makeMeasureSpec(
      MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
      heightMeasureSpec = MeasureSpec.makeMeasureSpec(
      MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

答案 7 :(得分:0)

使用Android.Support.V4.Widget; 使用Android.Util;

。 。

   class MyDrawerLayout: DrawerLayout
{
    public MyDrawerLayout(Context context) : base(context)
    {

    }
    public MyDrawerLayout(Context context, IAttributeSet attrs) : base(context, attrs)
    {

    }
    public MyDrawerLayout(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {

    }
    public MyDrawerLayout(IntPtr context, Android .Runtime.JniHandleOwnership jniHandleOwnership) : base(context, jniHandleOwnership)
    {

    }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        widthMeasureSpec = MeasureSpec.MakeMeasureSpec(MeasureSpec.GetSize(widthMeasureSpec), MeasureSpecMode.Exactly);
        heightMeasureSpec = MeasureSpec.MakeMeasureSpec(MeasureSpec.GetSize(heightMeasureSpec), MeasureSpecMode.Exactly);

        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);

    }




}

答案 8 :(得分:0)

错误地,我为活动设置了自定义对话框主题,而不是自定义活动主题:

activity.setTheme(R.style.StyledDialogTheme);

在我纠正之后,它再次运作。

答案 9 :(得分:0)

如果仍然出现此异常,请确保您的活动是全屏定义的 在清单中,您可以在Activity中进行操作:

android:theme="@style/Theme.AppCompat.Light.FullScreen"

或在styles.xml中定义:

 <style name="Theme.AppCompat.Light.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">false</item>
        <item name="android:windowActionBar">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

答案 10 :(得分:0)

即使我尝试了抽屉的layout_height =“ match_parent”,但是java也抛出了错误,所以我只是重新启动了android studio,却没有发现错误

答案 11 :(得分:-3)

必须构建 - > 清理项目然后重建项目错误将被解决