叽哩叽哩鸡 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 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名