使用textview和listview进行抽屉导航

时间:2013-07-02 09:50:35

标签: android navigation drawer

我正在尝试将TextView包含在Android的标准抽屉导航中。但我真的不知道如何使用它。这就是我到目前为止所做的。

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

我收到错误:No drawer view found with absolute gravity LEFT。有人能帮助我吗?

2 个答案:

答案 0 :(得分:2)

创建抽屉布局

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

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

此布局演示了一些重要的布局特征: 主内容视图(上面的FrameLayout)必须是DrawerLayout中的第一个子节点,因为XML顺序意味着z排序,抽屉必须位于内容之上。 主内容视图设置为与父视图的宽度和高度匹配,因为它表示隐藏导航抽屉时的整个UI。 抽屉视图(ListView)必须使用android:layout_gravityattribute指定其水平重力。要支持从右到左(RTL)语言,请使用&#34; start&#34;指定值。而不是&#34;左&#34; (因此当布局为RTL时,抽屉出现在右侧)。 抽屉视图以dp为单位指定其宽度,高度与父视图匹配。抽屉宽度不应超过320dp,因此用户始终可以看到主要内容的一部分。 初始化抽屉列表&amp;抽屉Latyout

首先,您必须初始化抽屉列表和抽屉布局。然后必须使用适配器设置列表视图的值。

mColors = getResources().getStringArray(R.array.colors_array);
mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList=(ListView)findViewById(R.id.left_drawer);

收听打开和关闭事件

要监听抽屉打开和关闭事件,请在DrawerLayout上调用setDrawerListener()并将其传递给DrawerLayout.DrawerListener。此接口为抽屉事件提供回调,例如onDrawerOpened()和onDrawerClosed()。

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()
}
};

处理导航单击事件

当用户选择抽屉列表中的项目时,系统会在给予setOnItemClickListener()的OnItemClickListener上调用onItemClick()。

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);
}

在selectItem()中你必须将相关的片段调用到给定位置。这里我创建了简单的片段并改变了背景颜色和文本取决于给定的输入。

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;
}
}

使用ActionBarDrawerToggle时,必须在调用期间调用它 onPostCreate()和onConfigurationChanged()

@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);
}

如需更多参考,请访问:http://velmuruganandroidcoding.blogspot.in/2014/09/navigation-drawer-in-android.html

答案 1 :(得分:1)

我认为问题在于线性布局中的android:gravity =“left”.Docs建议使用layout_gravity =“start”,这应该符合您的要求。

相关问题