Android kitkat

编程入门 行业动态 更新时间:2024-10-28 20:16:46
Android kitkat - AppCompat:不在我的FrameLayout容器中加载片段。(使用导航抽屉)(Android kitkat - AppCompat: Doesn't load fragments in my FrameLayout container.(Using a Navigation drawer))

我使用AppCompat实现了一个带有Material Design的DrawerLayout,它运行正常。 问题是当我尝试在容器中加载片段时永远不会出现..而且我不知道为什么。

以下是我的活动课程

public class MyActivity extends ActionBarActivity { private Toolbar toolbar; private DrawerLayout drawerLayout; private ActionBarDrawerToggle drawerToggle; private ListView leftDrawerList; private ArrayAdapter<String> navigationDrawerAdapter; private String[] leftSliderData = {"Estaciones", "Favorito", "Contacta"}; private int mCurrentSelectedPosition = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); nitView(); if (toolbar != null) { setSupportActionBar(toolbar); } initDrawer(); } private void nitView() { leftDrawerList = (ListView) findViewById(R.id.left_drawer); toolbar = (Toolbar) findViewById(R.id.toolbar); drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); navigationDrawerAdapter=new ArrayAdapter<String>( MyActivity.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) { Log.e("TEST", position + ""); FragmentManager fragmentManager = getFragmentManager(); Fragment fragment = new Fragment(); switch(position) { case 0: fragment = new Estacion(); break; case 1: fragment = new Favorito(); break; case 2: fragment = new Contacta(); break; } fragmentManager.beginTransaction() .replace(R.id.container, fragment) .commit(); } }); } 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); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); drawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); drawerToggle.onConfigurationChanged(newConfig); }

}

我的xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity"> <include layout="@layout/toolbar" /> <android.support.v4.widget.DrawerLayout android:layout_width="match_parent" android:id="@+id/drawerLayout" android:layout_height="match_parent"> <!-- The main content view --> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- activity view --> <RelativeLayout android:layout_width="match_parent" android:background="#fff" android:layout_height="match_parent"> <TextView android:layout_centerInParent="true" android:layout_width="wrap_content" android:textColor="@android:color/white" android:text="Activity Content" android:layout_height="wrap_content" /> </RelativeLayout> <!-- navigation drawer --> <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>

谢谢!

I have implemented a DrawerLayout with Material Design, using AppCompat and it's working ok. The problem is when I try to load a fragment in the container never appear.. and I don't know why.

Below is my activity class

public class MyActivity extends ActionBarActivity { private Toolbar toolbar; private DrawerLayout drawerLayout; private ActionBarDrawerToggle drawerToggle; private ListView leftDrawerList; private ArrayAdapter<String> navigationDrawerAdapter; private String[] leftSliderData = {"Estaciones", "Favorito", "Contacta"}; private int mCurrentSelectedPosition = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); nitView(); if (toolbar != null) { setSupportActionBar(toolbar); } initDrawer(); } private void nitView() { leftDrawerList = (ListView) findViewById(R.id.left_drawer); toolbar = (Toolbar) findViewById(R.id.toolbar); drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); navigationDrawerAdapter=new ArrayAdapter<String>( MyActivity.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) { Log.e("TEST", position + ""); FragmentManager fragmentManager = getFragmentManager(); Fragment fragment = new Fragment(); switch(position) { case 0: fragment = new Estacion(); break; case 1: fragment = new Favorito(); break; case 2: fragment = new Contacta(); break; } fragmentManager.beginTransaction() .replace(R.id.container, fragment) .commit(); } }); } 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); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); drawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); drawerToggle.onConfigurationChanged(newConfig); }

}

my xml layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity"> <include layout="@layout/toolbar" /> <android.support.v4.widget.DrawerLayout android:layout_width="match_parent" android:id="@+id/drawerLayout" android:layout_height="match_parent"> <!-- The main content view --> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- activity view --> <RelativeLayout android:layout_width="match_parent" android:background="#fff" android:layout_height="match_parent"> <TextView android:layout_centerInParent="true" android:layout_width="wrap_content" android:textColor="@android:color/white" android:text="Activity Content" android:layout_height="wrap_content" /> </RelativeLayout> <!-- navigation drawer --> <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>

Thanks!

最满意答案

抽屉的主要内容视图必须是一个ViewGroup因此您必须将所有视图作为一个视图组的子视图放置

<!-- The main content view --> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" />

作为一个孩子

<!-- activity view --> <RelativeLayout android:layout_width="match_parent" android:background="#fff" android:layout_height="match_parent"> <TextView android:layout_centerInParent="true" android:layout_width="wrap_content" android:textColor="@android:color/white" android:text="Activity Content" android:layout_height="wrap_content" /> </RelativeLayout>

your main content view of drawer must be one ViewGroup so you must put all your views as children of one viewgroup so put

<!-- The main content view --> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" />

as a child of

<!-- activity view --> <RelativeLayout android:layout_width="match_parent" android:background="#fff" android:layout_height="match_parent"> <TextView android:layout_centerInParent="true" android:layout_width="wrap_content" android:textColor="@android:color/white" android:text="Activity Content" android:layout_height="wrap_content" /> </RelativeLayout>

更多推荐

android,void,id,电脑培训,计算机培训,IT培训"/> <meta name="description&q

本文发布于:2023-08-04 08:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412254.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   kitkat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!