Android Studio 0.8.10
你好
我正在尝试实现抽屉导航,但我一直收到此错误。该 fragment 扩展了 ListFragment 并且我已经覆盖了 getview 以扩充下面的布局。我有一个添加 fragment 并初始化 DrawLayar 类的 Activity 。
我正在使用抽屉导航并具有以下布局 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);
}
我希望有人能给我指明正确的方向,因为这几天一直是个问题。
提前致谢
最佳答案
在本例中,在 getView()
方法中扩展的布局是使用 NewsFeedAdapter
的 ListView 的各个行的布局。由于 R.layout.fragment_listfott
包含适用于应用程序抽屉导航的 DrawerLayout,因此不应将布局扩展并返回到那里。相反,该布局应该是定义行的布局,对于简单的 ArrayAdapter,通常类似于 SDK 的 android.R.layout.simple_list_item_1
,它只是一个 TextView。
关于android - DrawerLayout 必须使用 MeasureSpec.EXACTLY 进行测量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25864323/