将 Tab 设为滚动条并仅显示 6 个选项卡中的 3 个
我试图将我的选项卡设置为滚动条。我有 6 个选项卡,但想在 UI 中显示 3 个。如何设置这种格式的滚动条视图?如果可以这样设置吗?
标签栏基于 Android 开发者页面。
感谢
<?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">
<ScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</ScrollView>
Java 文件
package developers.tab;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class AndroidDevelopersTabActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, WorkoutActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("workouts").setIndicator("Workout",
res.getDrawable(R.drawable.ic_tab_workout))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ExcerciseActivity.class);
spec = tabHost.newTabSpec("excercises").setIndicator("Excercises",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ProgramsActivity.class);
spec = tabHost.newTabSpec("programs").setIndicator("Programs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, LogActivity.class);
spec = tabHost.newTabSpec("mealplans").setIndicator("Meal Plans",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, BodyMActivity.class);
spec = tabHost.newTabSpec("bodymeasurements").setIndicator("Body Measurements",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MealPlanActivity.class);
spec = tabHost.newTabSpec("mealplan").setIndicator("Meal Plan",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
Im trying to set my tabs as a scrollbar. I have 6 tabs but want to show 3 in the UI. How do I set the scrollbar view in this format? If it even possible to set it like this?
Tab Bar is based on androids developer page.
Thanks
<?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">
<ScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</ScrollView>
Java file
package developers.tab;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class AndroidDevelopersTabActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, WorkoutActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("workouts").setIndicator("Workout",
res.getDrawable(R.drawable.ic_tab_workout))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ExcerciseActivity.class);
spec = tabHost.newTabSpec("excercises").setIndicator("Excercises",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ProgramsActivity.class);
spec = tabHost.newTabSpec("programs").setIndicator("Programs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, LogActivity.class);
spec = tabHost.newTabSpec("mealplans").setIndicator("Meal Plans",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, BodyMActivity.class);
spec = tabHost.newTabSpec("bodymeasurements").setIndicator("Body Measurements",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MealPlanActivity.class);
spec = tabHost.newTabSpec("mealplan").setIndicator("Meal Plan",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不能让不需要的选项卡的可见性“消失”?
当左右滚动时,您可以使侧边选项卡消失或可见。
通过
view.setVisibility=GONE;
或view.setVisibility=VISIBLE;
如果你执行 viewA GONE,它将不占任何位置。所有其他视图的行为就好像 viewA 不存在一样。听滚动,当有地方可以显示右侧选项卡时,显示它们并隐藏左侧选项卡。反之亦然。
Why couldn't you just make the visiblity of unwanted tabs "GONE"?
You can make the side tabs GONE or VISIBLE when scrolling from side to side.
By
view.setVisibility=GONE;
orview.setVisibility=VISIBLE;
If you do viewA GONE, it will take no place. All other views behave themselves as if viewA didn't exist. Listen to scrolling and when there is place for showing right tabs, show them, and hide the left ones. And vice versa.