【问题标题】:Can i have a view stub on top of TabActivity?我可以在 TabActivity 顶部有一个视图存根吗?
【发布时间】:2011-09-09 03:04:23
【问题描述】:

我的选项卡活动中有四个选项卡,并且无论当前选项卡选择如何,我都需要一个在选项卡活动顶部带有三个按钮的工具栏。

我可以为此使用viewstub吗?

谁能帮我实现这个目标?

【问题讨论】:

    标签: android view android-activity


    【解决方案1】:
    <?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">
        <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:id="@+id/header">
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="woopra" android:textColor="#ffffff" android:textSize="36sp"
                android:textStyle="bold"/>
        </LinearLayout>       
        <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
            <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:visibility="gone"/>            
            <LinearLayout android:layout_width="fill_parent" android:layout_height="64dip">
                <Button android:layout_height="fill_parent" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/dashboard_tab" 
                    android:onClick="tabHandler" android:text="Dashboard"/>
                <Button android:layout_height="fill_parent" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/visitors_tab" 
                    android:onClick="tabHandler" android:text="Vistors"/>
                <Button android:layout_height="fill_parent" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/chat_tab" android:onClick="tabHandler"
                    android:text="Chat"/>
                <Button android:layout_height="fill_parent" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/reports_tab" 
                    android:onClick="tabHandler" android:text="Reports"/>
            </LinearLayout> 
        </FrameLayout>
        <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0dip"
                     android:layout_weight="1.0"/>
    </LinearLayout>
    

    这里的 id 为 'tabcontent' 的框架布局是我们选项卡的内容所在的位置。无论选择哪个选项卡,您在此框架布局之外放置的任何内容都将在选项卡活动中。

    如果您查看 xml 的顶部,您会看到一个带有 id 'header' 的线性布局。这是我制作的标题布局,在选项卡布局中始终可见。您可以通过放置按钮而不是像我所做的那样放置文本视图来创建工具栏。

    【讨论】:

    • 设置这个答案。它将帮助未来的用户甚至您将来参考。
    【解决方案2】:

    把你的工具栏放到布局里,有什么问题?

    【讨论】:

    • 如何将工具栏定义为 tabactivity 的布局?
    • 进行常规布局,并将tabhost.的id设置为@android:id/tabs。实际上,默认情况下它应该具有该 id。
    • 我有四个选项卡和四个活动以一种完善的方式。但除此之外,我还需要在与每个选项卡关联的每个活动顶部都有一个工具栏。
    猜你喜欢
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    相关资源
    最近更新 更多