Android导航抽屉失败

时间:2013-08-05 15:54:43

标签: android xml integration sidebar

我正在开发一个聊天应用程序,并想在GMail-App中添加一个导航抽屉。我是Java的新手,所以请不要杀了我:D我试图添加它但它崩溃了:(

这是我的Chat.java的一些代码

    public class Chat extends FragmentActivity {

final String[] data ={"Login","Register"};
final String[] fragments ={
        "com.linkr.chat.Login",
        "com.linkr.chat.Register"};
String username;
BufferedReader reader;
PrintWriter writer;
ArrayList<String> userList = new ArrayList();
public TextView chatTextArea;
TextView inputTextArea;
Button onClickButton;
Boolean isConnected = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setTitle("Linkr");
    /*<-- NAVDRAWER -->*/
    setContentView(layout.activity_chat);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);
    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    final ListView navList = (ListView) findViewById(R.id.drawer);
    navList.setAdapter(adapter);
    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
            drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                @Override
                public void onDrawerClosed(View drawerView){
                    super.onDrawerClosed(drawerView);
                    FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                    tx.replace(R.id.main, Fragment.instantiate(Chat.this, fragments[pos]));
                    tx.commit();
                }
            });
            drawer.closeDrawer(navList);
        }
    });
    FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
    tx.replace(R.id.main,Fragment.instantiate(Chat.this, fragments[0]));
    tx.commit();
    /*<-- NAVDRAWER END -->*/

我也没有得到如何使用NavigationDrawer。

这是我的activity_chat.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="300dp"
            android:layout_height="800dp"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context=".Chat"
            android:text = "Linkr">
<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="300dp"
                                        android:layout_height="800dp"
                                        tools:context=".MainActivity" >

    <ListView
            android:id="@+id/drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#FFF"
            android:choiceMode="singleChoice"/>

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

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/inputTextArea"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/onClickButton"/>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:id="@+id/onClickButton"
        android:layout_alignBaseline="@+id/inputTextArea"
        android:layout_alignBottom="@+id/inputTextArea"
        android:layout_alignParentRight="true"/>

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/chatTextArea"
        android:layout_above="@+id/onClickButton"
        android:layout_alignLeft="@+id/inputTextArea"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/onClickButton"/>

我需要另外的XML吗?谁能告诉我这是如何工作的? 感谢你们! :)

1 个答案:

答案 0 :(得分:1)

android.support.v4.widget.DrawerLayout设计为top ViewGroup,可以有两个孩子。第一个是你的Content View(可见的视图),就像普通的Activity布局一样。第二个是你的Drawer View(你可以从侧面拉出的视图)。

内容视图和抽屉视图既可以是像RelativeLayout这样的ViewGroup,也可以是ListView中的单个视图。

所以你的布局应该是这样的:

    <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="300dp"
        android:layout_height="800dp"
        tools:context=".MainActivity" >

         <!-- YOUR CONTENT VIEW-->
            <RelativeLayout 
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="300dp"
                android:layout_height="800dp"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".Chat"
                android:text = "Linkr">
        <EditText
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:id="@+id/inputTextArea"
             android:layout_alignParentBottom="true"
             android:layout_alignParentLeft="true"
             android:layout_toLeftOf="@+id/onClickButton"/>

        <Button
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Send"
             android:id="@+id/onClickButton"
             android:layout_alignBaseline="@+id/inputTextArea"
             android:layout_alignBottom="@+id/inputTextArea"
             android:layout_alignParentRight="true"/>

        <EditText
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:inputType="textMultiLine"
             android:ems="10"
             android:id="@+id/chatTextArea"
             android:layout_above="@+id/onClickButton"
             android:layout_alignLeft="@+id/inputTextArea"
             android:layout_alignParentTop="true"
             android:layout_alignRight="@+id/onClickButton"/>
        </RelativeLayout>

        <--! YOUR DRAWER VIEW -->
        <ListView
                android:id="@+id/drawer"
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:background="#FFF"
                android:choiceMode="singleChoice"/>

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