0

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

这是我的 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 吗?有人可以告诉我这是如何工作的吗?感谢你们!:)

4

1 回答 1

1

android.support.v4.widget.DrawerLayout设计成top ViewGroup可以有两个孩子。第一个是您的Content View(可见的视图),就像正常的活动布局一样。第二个是您的Drawer View(您可以从侧面拉出的视图)。

Content View 和 Drawer View 都可以是一个 ViewGroup,如 RelativeLayout 或在您的情况下为 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>
于 2013-08-05T16:07:47.073 回答