叽哩叽哩鸡 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 为什么我按照电路图做出的仿真和实物都不能使用
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web