我已经开始开发一个 android 应用程序,它包含以下内容:
- MainActivity:扩展 FragmentActivity。
- 几个 fragment :扩展 android.support.v4.app.Fragment 每一个。
MainActivity 从应用程序开始,它有一个 ActionBarDrawer(这是客户端实现抽屉式菜单的要求)。此外,MainActivity 进行 fragment 事务:当抽屉中的 X 选项被点击时,MainActivity 替换并设置所需的 fragment (应用程序启动时设置默认 fragment )。
这张图说明了这一点:
我的类(class):
public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
// that is the one which is deprecated: ActionBarDrawerToggle mDrawerToggle:
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mDrawerMenuTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
public class FragmentEuskalmet extends Fragment implements android.widget.AdapterView.OnItemSelectedListener {
activity_main.xml 布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
drawer_list_item.xml 布局:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
我意识到随着 Android 5.0 的发布,ActionBar 已被弃用。所以我的问题如下:如何更改我的应用程序以支持新的工具栏,使用其新的抽屉导航(替换已弃用的抽屉)并在 MainActivity 中维护我的 Fragment 使用(使用 FragmentActivity)?
在此先感谢您的帮助!
最佳答案
ActionBar 在 Android 5.0 中未弃用。
从 Android 5.0(API 级别 21)开始,操作栏可以由应用程序布局中的任何工具栏小部件表示。
也就是说,要用新的工具栏和新的 appcompat v21
替换 ActionBar,您必须:
- 扩展
AppCompatActivity
而不是FragmentActivity
- 在需要时使用
getSupportActionBar()
而不是getActionBar()
- 使用继承自 Theme.AppCompat 的主题。(例如 Light 或 NoActionBar)
- 将工具栏 View 放在您的布局中
更多信息 here:
然后集成 navDrawer 你可以这样做:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
mDrawerToggle= new ActionBarDrawerToggle(this, mDrawerLayout,mToolbar, R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
关于android - 如何在android应用中实现Navigation Drawer + 全新的Toolbar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26777223/