TabHost与水平滚动发行 [英] TabHost with Horizontal Scroll Issue

查看:194
本文介绍了TabHost与水平滚动发行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有不同数目的标签,我使用​​TabHost。 这些可以是1之间至8中数取决于数据

我想有添加水平滚动,这样,当所有的8在那里它看起来并不拥挤了。

问题是,当5个或更多是有它看起来很好,滚动的作品! 但是,当标签数量少我看到的空白。选项​​卡没有得到拉伸以填补额外的空间。

我该如何解决? 这可以通过Java code做些什么呢?

这是我的布局XML ...

 < XML版本=1.0编码=UTF-8&GT?;
< TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@机器人:ID / tabhost
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>

<的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    <!---其他浏览 - >

    < Horizo​​ntalScrollView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>

        < TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT/>
    < / Horizo​​ntalScrollView>

    <的FrameLayout
        机器人:ID =@机器人:ID / tabcontent
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>

< / TabHost>
 

解决方案

我增加了三个选项卡,并没有问题,使用它,如果你想让它滚动只是添加< Horizo​​ntalScrollView> 作为父< TabWidget> mainActivity.xml

 < XML版本=1.0编码=UTF-8&GT?;
< TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>

        <的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_above =@机器人:ID /标签/>

        < TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentBottom =真/>
    < / RelativeLayout的>

< / TabHost>
 

MainActivity

 进口android.app.TabActivity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.widget.TabHost;
进口android.widget.TabHost.OnTabChangeListener;
进口android.widget.TabHost.TabSpec;

@燮pressWarnings(德precation)
公共类MainActivity扩展TabActivity实现OnTabChangeListener
{
    TabHost tabHost;

    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.activity_main);

        tabHost = getTabHost();

        则tabspec photospec = tabHost.newTabSpec(家);
        photospec.setIndicator();
        意图photosIntent =新的意图(这一点,Download.class);
        photospec.setContent(photosIntent);

        则tabspec songspec = tabHost.newTabSpec(诗经);
        songspec.setIndicator();
        意图songsIntent =新的意图(这一点,Home.class);
        songspec.setContent(songsIntent);

        则tabspec videospec = tabHost.newTabSpec(视频);
        videospec.setIndicator();
        意图videosIntent =新的意图(这一点,Album.class);
        videospec.setContent(videosIntent);

        tabHost.addTab(photospec);
        tabHost.addTab(songspec);
        tabHost.addTab(videospec);

        tabHost.getTabWidget()getChildAt(0).setBackgroundResource(R.drawable.download_unselect)。
        tabHost.getTabWidget()getChildAt(1).setBackgroundResource(R.drawable.main_selected)。
        tabHost.getTabWidget()getChildAt(2).setBackgroundResource(R.drawable.albums_unselect)。

        tabHost.getTabWidget()getChildAt(0).getLayoutParams()高度= 50。;

        tabHost.getTabWidget()getChildAt(1).getLayoutParams()高度= 70。;

        tabHost.getTabWidget()getChildAt(2).getLayoutParams()高度= 50。;

        tabHost.setCurrentTab(1);

        tabHost.setOnTabChangedListener(本);
    }

    @覆盖
    公共无效onTabChanged(字符串标签)
    {
        INT指数= tabHost.getCurrentTab();

        如果(指数== 0)
        {
            tabHost.getTabWidget()getChildAt(0).setBackgroundResource(R.drawable.download_selected)。
            tabHost.getTabWidget()getChildAt(1).setBackgroundResource(R.drawable.main_unselect)。
            tabHost.getTabWidget()getChildAt(2).setBackgroundResource(R.drawable.albums_unselect)。
        }
        否则,如果(指数== 1)
        {
            tabHost.getTabWidget()getChildAt(0).setBackgroundResource(R.drawable.download_unselect)。
            tabHost.getTabWidget()getChildAt(1).setBackgroundResource(R.drawable.main_selected)。
            tabHost.getTabWidget()getChildAt(2).setBackgroundResource(R.drawable.albums_unselect)。
        }
        否则,如果(指数== 2)
        {
            tabHost.getTabWidget()getChildAt(0).setBackgroundResource(R.drawable.download_unselect)。
            tabHost.getTabWidget()getChildAt(1).setBackgroundResource(R.drawable.main_unselect)。
            tabHost.getTabWidget()getChildAt(2).setBackgroundResource(R.drawable.albums_selected)。
        }
    }

}
 

I want to have different number of tabs and I am using TabHost. these could be between 1 to 8 in number depending on data.

I want to have horizontal scroll added so that when all 8 are there it doesn't look cramped up.

Issue is when 5 or more are there it looks fine and scroll works ! But when number of tabs are less I see blank space. tabs are not getting stretched to fill in the extra space.

How can I resolve this ? Can this be done via Java code ?

here is my layout xml...

<?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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!---Other Views--->

    <HorizontalScrollView
        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" />
    </HorizontalScrollView>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

</TabHost>

解决方案

I have added three tabs and have no problem using it and if you want to make it scrollable just Add <HorizontalScrollView> as a parent to <TabWidget> mainActivity.xml

<?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="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        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"
            android:layout_above="@android:id/tabs" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>

</TabHost>

MainActivity

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnTabChangeListener
{
    TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        tabHost = getTabHost();

        TabSpec photospec = tabHost.newTabSpec("Home");
        photospec.setIndicator("");
        Intent photosIntent = new Intent(this, Download.class);
        photospec.setContent(photosIntent);

        TabSpec songspec = tabHost.newTabSpec("Songs");        
        songspec.setIndicator("");
        Intent songsIntent = new Intent(this, Home.class);
        songspec.setContent(songsIntent);

        TabSpec videospec = tabHost.newTabSpec("Videos");
        videospec.setIndicator("");
        Intent videosIntent = new Intent(this, Album.class);
        videospec.setContent(videosIntent);

        tabHost.addTab(photospec); 
        tabHost.addTab(songspec); 
        tabHost.addTab(videospec);

        tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_unselect);
        tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_selected);
        tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_unselect);

        tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 50;

        tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 70;

        tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = 50;

        tabHost.setCurrentTab(1);

        tabHost.setOnTabChangedListener(this);
    }

    @Override
    public void onTabChanged(String tab) 
    {
        int index = tabHost.getCurrentTab();

        if(index == 0)
        {
            tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_selected);
            tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_unselect);
            tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_unselect);
        }
        else if(index == 1)
        {
            tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_unselect);
            tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_selected);
            tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_unselect);
        }
        else if(index == 2)
        {
            tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_unselect);
            tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_unselect);
            tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_selected);
        }
    }

}

这篇关于TabHost与水平滚动发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆