【问题标题】:NavigationDrawer Fragment Styling/IconNavigationDrawer 片段样式/图标
【发布时间】:2014-10-29 20:29:13
【问题描述】:

我也尝试了一些其他的 stackoverflow 和 google-solutions,但没有成功 (How to properly design/style a Android Navigation Drawer, Navigation Drawer – Part 1)

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

  • 标准
  • 活动物品
  • 按下/聚焦的项目

我还想在列表项之前添加一个图标!

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

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)。

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

【问题讨论】:

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


    【解决方案1】:

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

    mDrawerListView.setAdapter(new myCustomAdapter);
    

    或者,我曾经做过的,不要为您的抽屉片段使用列表视图:

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

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

    希望这会有所帮助。

    【讨论】:

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