tabhost-Android 中 TabHost 的 TabWidget 显示问题

问题描述

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>

显示为:

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

解决方案

终于找到大神相助

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

解决方案二:
这样是ok的:
<?xml version=""1.0"" encoding=""utf-8""?>
android:id=""@android:id/tabhost""
android:layout_width=""match_parent""
android:layout_height=""match_parent"" >

<LinearLayout    android:layout_width=""match_parent""    android:layout_height=""match_parent""    android:orientation=""vertical"" >    <FrameLayout        android:id=""@android:id/tabcontent""        android:layout_width=""match_parent""        android:layout_height=""match_parent"" >    </FrameLayout>    <TabWidget        android:id=""@android:id/tabs""        android:layout_width=""match_parent""        android:layout_height=""wrap_content""        android:gravity=""right|center_vertical"" /></LinearLayout>

解决方案三:
这是我很久以前联系tab的一个demo,你对比看看
主布局文件:
<?xml version=""1.0"" encoding=""utf-8""?>
android:layout_width=""match_parent""
android:layout_height=""match_parent""
android:orientation=""vertical"">
<!--
TabHost的id为""@+id/tabhost""
-->
android:id=""@+id/tabhost""
android:layout_width=""match_parent""
android:layout_height=""match_parent"">

    <LinearLayout        android:layout_width=""match_parent""        android:layout_height=""match_parent""        android:orientation=""vertical"">        <TabWidget            android:id=""@android:id/tabs""            android:layout_width=""match_parent""            android:layout_height=""wrap_content""            android:layout_marginTop=""1dp"">        </TabWidget>        <FrameLayout            android:id=""@android:id/tabcontent""            android:layout_width=""match_parent""            android:layout_height=""match_parent"">            <LinearLayout                android:id=""@+id/tab1""                android:layout_width=""match_parent""                android:layout_height=""match_parent"">                <TextView                    android:id=""@+id/tab1_txt""                    android:layout_width=""match_parent""                    android:layout_height=""match_parent""                    android:text=""the content of tab1""                    android:textSize=""20sp""/>            </LinearLayout>            <LinearLayout                android:id=""@+id/tab2""                android:layout_width=""match_parent""                android:layout_height=""match_parent"">                <TextView                    android:id=""@+id/tab2_txt""                    android:layout_width=""match_parent""                    android:layout_height=""match_parent""                    android:text=""the content of tab2""                    android:textSize=""20sp""/>            </LinearLayout>            <LinearLayout                android:id=""@+id/tab3""                android:layout_width=""match_parent""                android:layout_height=""match_parent"">                <TextView                    android:id=""@+id/tab3_txt""                    android:layout_width=""match_parent""                    android:layout_height=""match_parent""                    android:text=""the content of tab3""                    android:textSize=""20sp""/>            </LinearLayout>        </FrameLayout>    </LinearLayout></TabHost>

主Activity文件:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost1);

    TabHost tabHost = (TabHost) MyTab1.this.findViewById(R.id.tabhost);    tabHost.setup();    TabHost.TabSpec tabSpec01 = tabHost.newTabSpec(""tab1"");    tabSpec01.setIndicator(""firsttab"" MyTab1.this.getResources().getDrawable(R.drawable.ic_launcher));    tabSpec01.setContent(R.id.tab1);    TabHost.TabSpec tabSpec02 = tabHost.newTabSpec(""tab2"");    tabSpec02.setIndicator(""secondtab"" MyTab1.this.getResources().getDrawable(R.drawable.ic_launcher));    tabSpec02.setContent(R.id.tab2);    TabHost.TabSpec tabSpec03 = tabHost.newTabSpec(""tab3"");    tabSpec03.setIndicator(""thirdtab"" MyTab1.this.getResources().getDrawable(R.drawable.ic_launcher));    tabSpec03.setContent(R.id.tab3);    tabHost.addTab(tabSpec01);    tabHost.addTab(tabSpec02);    tabHost.addTab(tabSpec03);}

解决方案四:
我觉得你是水平放置按钮的,应该把TabWidget 的宽高改成 android:layout_width=""0dp"" android:layout_height=""wrap_content"",

不知道是不是这个问题,可以试试

解决方案五:
Tab过时了,看看我的博客:
http://blog.csdn.net/logicteamleader/article/details/45487263

时间: 2024-08-01 18:32:57

tabhost-Android 中 TabHost 的 TabWidget 显示问题的相关文章

Android 中 TabHost与ViewPager结合实现首页导航效果_Android

今天发的是TabHost结合ViewPager实现首页底部导航的效果,虽然说网上有很多这样的Demo,不过呢,我还是要把自己练习写的发出来,没错!就是这么任性: 先上效果图,如下: 代码里面有注释,就不过多解释了,说几点需要注意的问题 1:TabHost .TabWidget.FrameLayout一定添加id这个属性,否则会报错 android:id="@android:id/tabhost" android:id="@android:id/tabcontent"

Android 中 TabHost与ViewPager结合实现首页导航效果

今天发的是TabHost结合ViewPager实现首页底部导航的效果,虽然说网上有很多这样的Demo,不过呢,我还是要把自己练习写的发出来,没错!就是这么任性: 先上效果图,如下: 代码里面有注释,就不过多解释了,说几点需要注意的问题 1:TabHost .TabWidget.FrameLayout一定添加id这个属性,否则会报错 android:id="@android:id/tabhost" android:id="@android:id/tabcontent"

设置宽度-Android中使用mupdf横屏显示pdf时将pdf缩放了,哪位大神用过这个库?

问题描述 Android中使用mupdf横屏显示pdf时将pdf缩放了,哪位大神用过这个库? 在横屏阅读pdf时,将pdf纵向的缩小显示了整个pdf.但是现在要的效果就是横屏的时候让pdf的宽达到横屏时候的宽,(目前是横向滑动的)高度可以上下来回拖!可以两根手指放大之类的效果,请问有哪位大神用过这个pdf,如何设置初始时候的画面大小啊,求大神解答,在线等!!! 解决方案 使用skia库生成pdf文件 解决方案二: 我的Mupdf嵌到项目中总是出错,是不是需要NDK编译啊,求指点:22313717

android开发-关于android中listview里面textview显示混乱

问题描述 关于android中listview里面textview显示混乱 哪位大神能帮我解决下面代码中的listview里面textview的显示混乱问题 纠结好久了 谢谢 复制可以直接执行 源文件: package com.example.listviewtest; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import android.view.LayoutInfla

Android中实现EditText密码显示隐藏的方法

在Google发布了support:design:23+以后我们发现有这么一个东西TextInputLayout,先看下效果图: <android.support.design.widget.TextInputLayout android:id="@+id/pwdLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:passw

Android中ViewPager获取当前显示的Fragment

前言 在项目中,有时会用到在ViewPager中显示同样类型的Fragment,同时这样的Fragment的个数是动态的,但是PagerAdapter没有给我们提供getCurrentFragment类似的方法.下面就给大家介绍下Android中ViewPager获取当前显示的Fragment的方法,一起看看吧. 一.使用 getSupportFragmentManager().findFragmentByTag()方法 Viewpager + FragmentPagerAdapter 情况下

Android中ScrollView嵌套GridView显示不全解决方法

Android中ScrollView嵌套GridView显示不全解决方法 由于ScrollView和GridView这两款控件都自带滚动条,一起使用GridView会显示不全 解决方法:自定义gridview 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Android中TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题

最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. 下面说说本程序能够实现的功能: 实现TabHost中的标题栏能够横向滚动: 自定义标题栏的大小和样式: 自定义标题栏的分割线的样式: 下面分几步来分别实现以上的功能: 第一步,先实现一个基本的TabHost的展现 详细的说明可以在网上其它地方搜的,主要就是注意一点,控件的id的是固定的不能随便更改

android中TabHost的图标(48&amp;#215;48)和文字叠加解决方法_Android

开发过程中,有时候图标稍微大点,比如48×48的时候,文字就会和图标叠加起来,解决方法如下: 复制代码 代码如下: TabWidget tw = tabHost.getTabWidget(); for (int i = 0; i < tw.getChildCount(); i++) {     TextView tv=(TextView)tw.getChildAt(i).findViewById(android.R.id.title);     ImageView iv=(ImageView)t

android中TabHost的图标(48&#215;48)和文字叠加解决方法

开发过程中,有时候图标稍微大点,比如48×48的时候,文字就会和图标叠加起来,解决方法如下:复制代码 代码如下:TabWidget tw = tabHost.getTabWidget(); for (int i = 0; i < tw.getChildCount(); i++) {     TextView tv=(TextView)tw.getChildAt(i).findViewById(android.R.id.title);     ImageView iv=(ImageView)tw.