如何将滑动抽屉和标签小部件一起添加?

时间:2013-12-20 04:38:26

标签: android android-layout

我是android的新手。我想将滑动抽屉和标签小部件一起添加。任何人都可以帮助我吗?

这是我要创建的应用

enter image description here

我尝试将以下两个组件组合在一起。

滑动抽屉=>

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

    <!-- The main content view -->

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/drawer_text" ></TextView>
    </RelativeLayout>

    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/drawer"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#F3F3F4"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" ></ListView>

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

Tab Widget code =&gt;

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

    </LinearLayout>

</TabHost>

我能把这两个结合起来吗?

这是活动类

public class TestTab extends TabActivity implements OnTabChangeListener {


    TabHost tabHost;

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test_drawer);


            // Get TabHost Refference
              tabHost = getTabHost();

              // Set TabChangeListener called when tab changed
              tabHost.setOnTabChangedListener(this);

              TabHost.TabSpec spec;
              Intent intent;

               /************* TAB1 ************/
              // Create  Intents to launch an Activity for the tab (to be reused)
              intent = new Intent().setClass(this, Favourite.class);
              spec = tabHost.newTabSpec("First").setIndicator("")
                            .setContent(intent);

              //Add intent to tab
              tabHost.addTab(spec);

              /************* TAB2 ************/
              intent = new Intent().setClass(this, Offers.class);
              spec = tabHost.newTabSpec("Second").setIndicator("")
                            .setContent(intent);  
              tabHost.addTab(spec);

              /************* TAB3 ************/
              intent = new Intent().setClass(this, NearBy.class);
              spec = tabHost.newTabSpec("Third").setIndicator("")
                            .setContent(intent);
              tabHost.addTab(spec);

              // Set drawable images to tab
              tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.offertwo);
              tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.nearbytwo);

              // Set Tab1 as Default tab and change image   
              tabHost.getTabWidget().setCurrentTab(0);
              tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.favoritetwo);


           }

        @Override
        public void onTabChanged(String tabId) {

            /************ Called when tab changed *************/

            //********* Check current selected tab and change according images *******/

            for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
            {
                if(i==0)
                    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.favoritetwo);
                else if(i==1)
                    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.offertwo);
                else if(i==2)
                    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.nearbytwo);
            }


            Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());

        if(tabHost.getCurrentTab()==0)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.favourite);
        else if(tabHost.getCurrentTab()==1)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.offers);
        else if(tabHost.getCurrentTab()==2)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.nearby);

        }

2 个答案:

答案 0 :(得分:2)

您可以将Tab host小部件放在抽屉的内容部分,即relative view

试试这个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" >

<!-- The main content view -->


    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <TabHost
            android:id="@android:id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"/>

            </LinearLayout>

        </TabHost>
    </RelativeLayout>


    <ListView
        android:id="@+id/drawer"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#F3F3F4"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" ></ListView>
</android.support.v4.widget.DrawerLayout>

答案 1 :(得分:1)

试试这个。它完全适用于我的应用程序。我在滑动抽屉里面显示了一个列表视图。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relTapToCapture"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:visibility="visible" >

        <SlidingDrawer
            android:id="@+id/drawer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:content="@+id/content"
            android:handle="@+id/handle" >

            <ImageView
                android:id="@+id/handle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/sliding_handle" />

            <RelativeLayout
                android:id="@+id/content"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <ListView
                    android:id="@+id/listView_places"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/list_bg1"
                    android:divider="@drawable/border_left"
                    android:dividerHeight="1dp"
                    android:drawSelectorOnTop="true" >
                </ListView>
            </RelativeLayout>
        </SlidingDrawer>
    </FrameLayout>

</RelativeLayout>