2014-10-28 49 views
0

我也尝试了一些其他的计算器和谷歌的解决方案,但他们没有工作How to properly design/style a Android Navigation DrawerNavigation Drawer – Part 1NavigationDrawer片段造型/图标

我想要的风格,但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)。

那么如何改变风格?我不明白在哪里...

回答

1

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

mDrawerListView.setAdapter(new myCustomAdapter); 

或者说,我就是这样做一次,不要用一个ListView您drawerfragment:

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

在“R.layout.your_custom_fragment”你可以把任何你想要的至。如果ListView中的项目不是动态的,只需将一对TextViews放入垂直的LinearLayout中即可。

希望这有助于。