叽哩叽哩鸡 2015-06-27 08:02 采纳率: 100%
浏览 2105
已采纳

Android 中 TabHost 的 TabWidget 显示问题

当布局文件是这样的时候:

 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.smtsokt.activity.MainActivity" >

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="11" >
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal" >
        </TabWidget>
    </LinearLayout>
</TabHost>

显示为:
图片说明

TabWidget的宽度改为 wrap_content 时:

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal" >
        </TabWidget>

显示为:
图片说明

我怎样才能让这几个选项卡平铺在下方呢?

  • 写回答

5条回答

  • 叽哩叽哩鸡 2015-06-30 06:47
    关注

    终于找到大神相助

     private void setWeight(TabWidget tabWidget)
        {
            WindowManager windowManager = getWindowManager();
            Display windowDisplay = windowManager.getDefaultDisplay();
            int windowWidth = windowDisplay.getWidth();
            int tabWidth = windowWidth / 4; // 宽度比为:3:4:4:6:9
            tabWidget.getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams(tabWidth, LayoutParams.FILL_PARENT));
            tabWidget.getChildAt(1).setLayoutParams(new LinearLayout.LayoutParams(tabWidth, LayoutParams.FILL_PARENT));
            tabWidget.getChildAt(2).setLayoutParams(new LinearLayout.LayoutParams(tabWidth, LayoutParams.FILL_PARENT));
            tabWidget.getChildAt(3).setLayoutParams(new LinearLayout.LayoutParams(tabWidth, LayoutParams.FILL_PARENT));
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)