【问题标题】:DrawerLayout must be measured with MeasureSpec.EXACTLYDrawerLayout 必须使用 MeasureSpec.EXACTLY 进行测量
【发布时间】:2014-09-16 08:45:45
【问题描述】:
Android Studio 0.8.10

你好

我正在尝试实现导航抽屉,但我不断收到此错误。该片段扩展了 ListFragment 并且我已经覆盖了 getview 以扩展下面的布局。我有一个添加片段并初始化 DrawLayar 类的活动。 我正在使用导航抽屉并具有以下布局fragment_listfott.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dlMenus"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/navi_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/tvDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textColor="#ffc6c6c6"
            android:textSize="16sp" />

    </FrameLayout>

    <!-- Navigation drawer -->
    <LinearLayout
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/background"
        android:gravity="top"
        android:orientation="vertical">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:scaleType="fitXY"
        android:src="@drawable/logo_large"
        android:contentDescription="Picture displaying logo"/>
</LinearLayout>  
</android.support.v4.widget.DrawerLayout>

在我的FottListFragment.java 我有以下内容:

private class NewsFeedAdapter extends ArrayAdapter<NewsFeed> {

        public NewsFeedAdapter(ArrayList<NewsFeed> newsFeedArrayList) {
            super(getActivity(), 0, newsFeedArrayList);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.fragment_listfott, null);
            }

            return convertView;
        }
    }

MainActivity.java 我做所有 Drawlayer 的东西

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_listfott);
        if (savedInstanceState == null) {
        }

        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.navi_fragment_container);

        if (fragment == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.navi_fragment_container, new FottListFragment(), "FottListFragment")
                    .commit();
        }

        /* Navigation drawer */
        mDrawerLayout = (DrawerLayout)findViewById(R.id.dlMenus);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                R.drawable.menu_btn,
                R.string.open_draw,
                R.string.close_draw) {

            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

我希望有人能指出我正确的方向,因为这已经困扰了几天了。

在此先感谢

【问题讨论】:

  • 您不应该将 DrawerLayout 用于您的 ListView 行。也就是说,它不应该是您在 getView() 中膨胀的布局。
  • @MikeM。您是否认为 getView 不能与 DrawLayer 一起使用。我们的客户希望有一个可以在显示 ListView 项目时打开的 Navigation draw。
  • 适配器的getView() 方法,在这种情况下,返回ListView 行的视图。我认为您不希望 ListView 的每一行都有一个抽屉。使用简单的 ArrayAdapter,它通常是一个简单的布局,例如 android.R.layout.simple_list_item_1。您已经使用setContentView(R.layout.fragment_listfott) 创建主导航抽屉。
  • 我想我明白了。我将只为将在 getView 中膨胀的 listView 项目进行布局。但是,我需要另一个包含导航绘制的布局?
  • @MikeM 谢谢,一分钱终于掉了。如果你回答了这个问题,我会将你标记为正确。

标签: android navigation-drawer


【解决方案1】:

在这种情况下,getView() 方法中膨胀的布局是使用 NewsFeedAdapter 的 ListView 的各个行的布局。由于R.layout.fragment_listfott 包含用于应用程序导航抽屉的 DrawerLayout,因此不应将布局膨胀并返回到那里。相反,该布局应该是定义行的布局,对于简单的 ArrayAdapter,通常类似于 SDK 的 android.R.layout.simple_list_item_1,它只是一个 TextView。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多