如何在没有操作栏的情况下实现导航抽屉但是在主屏幕上用按钮打开导航屏幕?

时间:2013-12-29 09:04:43

标签: android navigation-drawer

导航抽屉应用程序在其left_drawer片段上有登录屏幕(请参阅链接:how to show a activity(login screen) under android navigation drawer),我想使用按钮从主屏幕打开此登录屏幕,也不希望导航栏上有操作栏抽屉。任何人都可以帮我这个吗?

提前谢谢!!

2 个答案:

答案 0 :(得分:5)

实际上很简单。比使用ActionBar更容易。我用几乎最简单的布局来写答案

让你的xml像这样:

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

<!-- This is how your main page will look, just 2 buttons -->

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:onClick="onLeft"
        android:text="left" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:onClick="onRight"
        android:text="right" />
    </RelativeLayout>

<!-- Left Drawrer -->

    <RelativeLayout
    android:id="@+id/whatYouWantInLeftDrawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_dark" />
    <!-- you can have many more widgets here like buttons or labels -->
    </RelativeLayout>

<!-- Right Drawrer -->

    <RelativeLayout
    android:id="@+id/whatYouWantInRightDrawer"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_green_light" />
    <!-- you can have many more widgets here like buttons or labels -->
    </RelativeLayout>

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

然后让你的活动像这样:

public class MainActivity extends Activity {

RelativeLayout leftRL;
RelativeLayout rightRL;
DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//      I'm removing the ActionBar.
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    leftRL = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
    rightRL = (RelativeLayout)findViewById(R.id.whatYouWantInRightDrawer);
    drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    }

    public  void onLeft(View view)
    {
    drawerLayout.openDrawer(leftRL);
    }

    public  void onRight(View view)
    {
    drawerLayout.openDrawer(rightRL);
    }
}

就是这样。希望它有所帮助。

答案 1 :(得分:0)

您可以使用导航抽屉。非常容易实现,非常专业。在导航抽屉处于活动状态时,只需创建另一个布局并使布局膨胀,而不是将列表视图放入导航抽屉中。您可以查看有关导航抽屉here的信息。

你会找到很多方法来摆出抽屉。但是你的问题是创建一个自定义布局并在导航抽屉内膨胀它。

祝你好运!