【问题标题】:Swipe effect in TabHost inside a fragment片段内 TabHost 中的滑动效果
【发布时间】:2017-04-02 01:06:44
【问题描述】:

如果单击按钮,我有一个 NavigationDrawer 会打开片段。但是现在,我需要在其中一个片段中创建一个 Tabhost。我从未使用过 TabHost,也找不到任何主题来解释如何在片段内的一个 TabHost 中实现 Swipe 效果。在那之前,我已经配置了 Tabhost,但没有任何效果。如果您能解释一下如何操作,我将不胜感激。

PacksFragment.java

package studio.com.archeagemanager;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 */


public class PacksFragment extends Fragment {

    TabHost tabHost;

    public PacksFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_packs, container, false);

        TabHost host = (TabHost) view.findViewById(R.id.tabhost);
        host.setup();

        //Tab 1
        TabHost.TabSpec spec = host.newTabSpec("Tab One");
        spec.setContent(R.id.nuia);
        spec.setIndicator("NUIA");
        host.addTab(spec);

        //Tab 2
        spec = host.newTabSpec("Tab two");
        spec.setContent(R.id.haranya);
        spec.setIndicator("HARANYA");
        host.addTab(spec);

        //Tab 3
        spec = host.newTabSpec("Tab three");
        spec.setContent(R.id.oceanic);
        spec.setIndicator("OCEANIC");
        host.addTab(spec);

        //Tab 4
        spec = host.newTabSpec("Tab four");
        spec.setContent(R.id.auroria);
        spec.setIndicator("AURORIA");
        host.addTab(spec);

        for (int i=0; i<4; i++) {
            View tabView = host.getTabWidget().getChildTabViewAt(i);
            tabView.setPadding(0, 0, 0, 0);
            TextView tv = (TextView)tabView.findViewById(android.R.id.title);
            tv.setSingleLine();
            tv.setTextSize(14);
        }

        return view;
    }

}

【问题讨论】:

    标签: java android android-fragments android-tabhost fragment-tab-host


    【解决方案1】:

    在您的布局文件fragments_pack 中,只需根据您的要求添加具有水平滚动视图的选项卡主机,类似于此

    <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"
        android:padding="5dp">
    
        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/horizontalScrollView"
            android:fillViewport="true"
            android:scrollbars="none">
    
            <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="fill_parent"
            android:padding="5dp" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多