android - 将 Tab 设为滚动条并仅显示 6 个选项卡中的 3 个

标签 android tabs scrollview android-tabhost

我正在尝试将选项卡设置为滚动条。我有 6 个选项卡,但想在 UI 中显示 3 个。如何设置这种格式的滚动条 View ?如果可以这样设置吗?

标签栏基于 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);
}
}

最佳答案

为什么不能让不需要的选项卡的可见性“消失”?

左右滚动时,您可以使侧边选项卡消失或可见。

通过 view.setVisibility=GONE;view.setVisibility=VISIBLE;

如果你确实查看了A GONE,它就不会占据任何位置。所有其他 View 的行为就好像 viewA 不存在一样。听滚动,当有地方可以显示右侧选项卡时,显示它们并隐藏左侧选项卡。反之亦然。

关于android - 将 Tab 设为滚动条并仅显示 6 个选项卡中的 3 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9252609/

相关文章:

tabs - Sublime Text不会在缩进时将制表符转换为空格...为什么以及如何解决它?

react-native - React Native Flatlist 不会在自定义动画底部工作表内滚动

android - 如何将相机中的图像保存到内存中?

java - 安卓枚举 : generate AAR binary with R8 on and throws "Function invocation ' name( )'" exception in client app

css - Bootstrap : prevent "nav-tabs nav-justified" from stacking on small screen

Android 如何从样式或主题更改选项卡颜色

Android FrameLayouts 并排与独立的内部滚动

java - FloatingActionButton - 滚动后不正确的 z-index

android - 应用程序在后台时未调用 Firebase onMessageReceived

android - setOnItemClickListener 未选择 ListView 项