2013-07-02 54 views
1

我想包括一個TextView標準的抽屜導航爲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> 

我收到一個錯誤:No drawer view found with absolute gravity LEFT。有人能幫助我嗎?

+0

http://www.androiddevelopersolution.com /2013/04/facebook-like-slide-in-and-slide-out.html –

回答

1

我認爲問題在於線性佈局中的android:gravity =「left」.Docs建議使用layout_gravity =「start」,並且應該滿足您的要求。

2

創建一個抽屜佈局

要添加導航抽屜,聲明與aDrawerLayout對象作爲佈局的根視圖的用戶界面。在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_gravity屬性指定其水平重力。要支持從右向左(RTL)語言,請使用「start」而不是「left」指定值(因此,當佈局爲RTL時,抽屜出現在右側)。 抽屜視圖以dp爲單位指定寬度,高度與父視圖匹配。抽屜寬度不應超過320dp,以便用戶始終可以看到主要內容的一部分。 初始化抽屜列表&抽屜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() 
} 
}; 

處理導航點擊活動

當用戶選擇在抽屜裏的列表中的項目,系統調用onItemClick()上theOnItemClickListener給setOnItemClickListener()。

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); 
} 
在選擇信息

(),你必須調用相關片段的給position.Here我創建簡單的片段和改變背景顏色和文字在依賴於給輸入。

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

+0

僅僅發佈一個文檔鏈接而不是複製/粘貼整個東西(和嘗試爲它獲得積分,a脹...)? – ygesher