带水平滚动的TabHost问题
问题描述:
我想要有不同数量的选项卡,并且我正在使用TabHost。 取决于数据,这些可能在1到8之间。带水平滚动的TabHost问题
我想添加水平滚动,以便当所有8个都在那里时,它看起来不会过于拥挤。
问题是当5个或更多有它看起来很好,滚动的作品! 但是,当标签数量少,我看到空白。标签不会被拉伸以填充额外的空间。
我该如何解决这个问题? 这可以通过Java代码完成吗?
这里是我的布局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>
答
这是为我工作。请尝试。
<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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tabStripEnabled="true"
android:orientation="horizontal" />
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
顺便说一句,它在哪里说TabHost被弃用?我没有得到任何警告...... 另外,依靠这个post,我相信TabHost是活着并且踢。
答
我增加了三个选项卡,并用它没有问题,如果你想让它滚动只是添加<HorizontalScrollView>
作为家长<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);
}
}
}
TabActivity和TabHost在android中被删除。你可以使用'ListFragment'而不是'TabActvity'。查看[这里](http://v4all123.blogspot.com/2013/04/fragments-in-android.html)简单的'ListFragment'教程。 – Gunaseelan 2013-04-26 10:17:37
使用版本2.3,所以我可以使用它 – 2013-04-26 11:37:47
是的。您可以使用。但是为什么'tabhost'被depricated在使用它时,你的活动可能会运行缓慢。那就是问题所在。 – Gunaseelan 2013-04-26 11:50:47