导航抽屉片段样式/图标

时间:2014-10-28 10:35:30

标签: java android xml android-layout android-fragments

我还尝试了其他一些stackoverflow和Google解决方案,但他们没有工作How to properly design/style a Android Navigation DrawerNavigation Drawer – Part 1

我想设置NavigationDrawer的样式,但我不明白,我应该更改哪些代码。 我想设置以下样式(背景/前景):

  • 标准
  • 有源项目
  • 压/聚焦-项目

我还想在List-Items之前添加一个Icon!

以下是加载内容并显示抽屉的活动:

activity_main_window.xml:

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<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"
    tools:context="bernd.slider">
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The drawer is given a fixed width in dp and extends the full height of
         the container. -->
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="bernd.slider.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>

这里的 fragment_navigation_drawer.xml:(存储ListView的地方)

<ListView 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:choiceMode="singleChoice"
   android:divider="@color/darkred"
    android:dividerHeight="1dp"
    android:background="@color/white"
    tools:context="bernd.slider.NavigationDrawerFragment"
    android:id="@+id/navigation_drawer" />

我应该提一下这段代码:

来自main_window.java的

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_area);
    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();
    onSectionAttached(1); // Set the title to Homepage
    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}
来自NavigationDrawerFragment.java的

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mDrawerListView = (ListView) inflater.inflate(
            R.layout.fragment_navigation_drawer, container, false);
    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    });
    mDrawerListView.setAdapter(new ArrayAdapter<String>(
            getActionBar().getThemedContext(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
            }));
    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
    return mDrawerListView;
}

重要的是我使用FragmentManager将特定页面中的内容加载到R.id.container(activity_main_window.xml)。

那么我怎样才能改变风格呢?我不明白... ...

1 个答案:

答案 0 :(得分:1)

您可以更改ListViewAdapter中的样式。我会考虑制作一个自定义适配器并从BaseAdapter扩展它。然后你可以去:

mDrawerListView.setAdapter(new myCustomAdapter);

或者,我曾做过一次,不要使用listview来进行抽屉碎片:

mDrawerListView = (ListView) inflater.inflate(
        R.layout.your_custom_fragment, container, false);

在“R.layout.your_custom_fragment”中你可以放任何你想要的东西。如果ListView中的项目不是动态的,只需将几个TextView放在一个垂直的LinearLayout中。

希望这会有所帮助。