gpt4 book ai didi

android - DrawerLayout 必须使用 MeasureSpec.EXACTLY 错误进行测量

转载 作者:IT老高 更新时间:2023-10-28 23:01:48 30 4
gpt4 key购买 nike

我正在尝试实现抽屉导航,但我不断收到此错误。我看到了类似的问题,但对我没有用。我有以下布局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);
}

最佳答案

我的 DrawerLayout 在 LinearLayout 内:

<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>

然后我通过将 layout_height="wrap_content" 更改为 LinearLayoutlayout_height="match_parent" 解决了问题。所以代码是:

<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="match_parent"
android:orientation="vertical"
android:background="@drawable/abohawawallpapersqr"
tools:context=".MainActivity">

关于android - DrawerLayout 必须使用 MeasureSpec.EXACTLY 错误进行测量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31746072/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com