【问题标题】:How to add sliding drawer and tab widget together?如何一起添加滑动抽屉和标签小部件?
【发布时间】:2013-12-20 04:38:26
【问题描述】:

我是安卓新手。我想一起添加滑动抽屉和标签小部件。谁能帮帮我??

这是我要创建的应用程序

我尝试结合以下两个组件。

滑动抽屉=>

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

标签小部件代码=>

<?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);

        }

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您可以将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>
    

    【讨论】:

    • 我试过了,但它说“错误:解析 XML 时出错:文档元素后出现垃圾”
    • 12-20 11:07:17.919: E/AndroidRuntime(5915): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.rp/com.example.rp.TestTab} :android.view.InflateException:二进制 XML 文件第 1 行:错误膨胀类 android.support.v4.widget.DrawerLayout 12-20 11:07:17.919:E/AndroidRuntime(5915):在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2114) 12-20 11:07:17.919: E/AndroidRuntime(5915): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
    • 我仅使用此代码成功运行了应用程序..显示您的活动代码
    【解决方案2】:

    试试这个。它完美地在我的应用程序上运行。我在滑动抽屉内显示一个列表视图。

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

    【讨论】:

    • 不,亲爱的。我想一起添加滑动抽屉和标签小部件。首先滑动抽屉,然后标签小部件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多