Android 带textview和listview的抽屉导航

Android 带textview和listview的抽屉导航,android,navigation,drawer,Android,Navigation,Drawer,我试图在android的标准抽屉导航中加入一个TextView。但我真的不知道如何使用它。这就是我到目前为止所做的 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" androi

我试图在android的标准抽屉导航中加入一个
TextView
。但我真的不知道如何使用它。这就是我到目前为止所做的

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 

    <LinearLayout
        android:layout_width="240dp"
        android:layout_height="match_parent" 
        android:gravity="left"
        >

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="Algemeen"
            android:background="@drawable/title_menu"
            android:textColor="#94A1A1"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            android:minHeight="?android:attr/listPreferredItemHeightSmall"  
            android:layout_weight="1"  
            android:gravity="start"     
        />  

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#323232"
            android:layout_weight="1"
            android:gravity="start"
        />

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>


我认为问题在于线性布局中的安卓:gravity=“left”。文档建议使用layout\u gravity=“start”,这应该满足您的要求。

创建抽屉布局

要添加导航抽屉,请使用aDrawerLayout对象声明用户界面作为布局的根视图。在DrawerLayout内部,添加一个包含屏幕主要内容的视图(抽屉隐藏时的主布局)和另一个包含导航抽屉内容的视图

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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"/>
</android.support.v4.widget.DrawerLayout>
倾听打开和关闭的事件

要侦听抽屉打开和关闭事件,请在您的抽屉布局上调用setDrawerListener(),并将DrawerLayout.DrawerListener的实现传递给它。此接口为抽屉事件(如onDrawerOpened()和onDrawerClosed())提供回调

处理导航单击事件

当用户选择抽屉列表中的一个项目时,系统将调用给setOnItemClickListener()的onItemClick Listener()上的onItemClick()

使用ActionBarDrawerToggle时,必须在 onPostCreate()和onConfigurationChanged()


更多参考信息请访问:

只发布文档链接,而不是复制/粘贴整个文档(并尝试为其获取分数…),这不是更人性化一点吗?
mColors = getResources().getStringArray(R.array.colors_array);
mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList=(ListView)findViewById(R.id.left_drawer);
mDrawerToggle=new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close){
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
public class DrawerItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
// TODO Auto-generated method stub
selectItem(position);
}
}
private void selectItem(int position){
Fragment fragment=new ColorsFragment();
Bundle bundle=new Bundle();
bundle.putInt("Position", position);
fragment.setArguments(bundle);
FragmentManager fragmentManager=getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
Toast.makeText(this, position+"", Toast.LENGTH_SHORT).show();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
public class ColorsFragment extends Fragment{
private int[] colors;
private int position;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview=inflater.inflate(R.layout.fragment_layout, container,false);
position=getArguments().getInt("Position");
RelativeLayout layout=(RelativeLayout)rootview.findViewById(R.id.layout);
TextView textView=(TextView)rootview.findViewById(R.id.textview);
colors=getActivity().getResources().getIntArray(R.array.colors);
textView.setText(getResources().getStringArray(R.array.colors_array)[position]);
layout.setBackgroundColor(colors[position]);
return rootview;
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}