在TabView中添加两个TextView

时间:2016-02-01 19:44:27

标签: java android tabs textview tabview

我正在尝试为这个选项卡添加类似的选项卡布局,其中一天中的日期位于选项卡底部的月份和日期。 Layout

我只需要一些关于如何做到这一点的想法。我虽然在选项卡中添加了两个TextView但却无法使其工作。这是我到目前为止所做的。

private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setElevation(0);

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupNumTab();
    }
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new OneFragment(), "ONE", 1);
        adapter.addFrag(new TwoFragment(), "TWO", 2);
        adapter.addFrag(new ThreeFragment(), "THREE", 3);
        adapter.addFrag(new FourFragment(), "FOUR", 4);
        adapter.addFrag(new FiveFragment(), "FIVE", 5);
        viewPager.setAdapter(adapter);
    }

    public void setupNumTab() {
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        TextView nameView = new TextView(this);
        nameView.setText("TEST");
        layout.addView(nameView);
        TextView anotherView = new TextView(this);
        anotherView.setText("12");
        layout.addView(anotherView);
        setContentView(layout);
    }

1 个答案:

答案 0 :(得分:0)

一种方法是使用 TabLayout ,对于每个项目,使用customView。

然后将其附加到viewPager。

如果您要显示多个标签,请转到 fragmentStatePagerAdapter ,或者如果您只有2个或3个标签,请使用 fragmentPagerAdapter 。你可以更多地阅读它们。

做这样的事......

假设这是您的一个mainActivity xml布局。

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="61.8dp"
    android:background="#06acef"
    android:elevation="0dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    app:elevation="0dp"
    app:tabGravity="center"
    app:tabIndicatorColor="@color/white"
    app:tabMode="fixed"
    app:tabPaddingEnd="-1dp"
    app:tabPaddingStart="-1dp"
    android:fillViewport="false">
</android.support.design.widget.TabLayout>

   <ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ViewPager>

他们的父母可以是 LinearLayout 或者说 RelativeLayout 。这取决于你如何做到这一点。

现在,为您的标签再创建一个布局。说 tab_layout.xml

<TextView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<TextView
    android:layout_below="@id/text2"
    android:id="@+id/show_time_tab_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

现在进入你的主要活动..

onCreate()回调

   ViewPageAdapter viewPageAdapter = new ViewPageAdapter(getSupportFragmentManager(), getApplicationContext(),this);

        mViewPager.setAdapter(viewPageAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

       // bind each tab data here
        for (int i = 0; i < range; i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            tab.setCustomView(viewPageAdapter.getTabView(i));
        }

现在,您的ViewPageAdapter

public class ViewPageAdapter extends FragmentStatePagerAdapter{
    @Override
    public Fragment getItem(int position) {

       return YoutFragment.newInstance();
    }

    @Override
    public int getCount() {
        return 10;
    }

    public View getTabView(int position) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View tabView = layoutInflater.inflate(R.layout.your_custom_tab_layout, null, false);
        TextView text1 = (TextView) tabView.findViewById(R.id.text1);
        TextView text2 = (TextView) tabView.findViewById(R.id.text2);
        text1.setText("text1");
        text2.setText("dateStr[1]");
        return tabView;
    }
}

最后,你的片段,每个viewpagerAdapter,在 getItem()回调中调用。

public class YourFragment extends Fragment{
      public static YourFragment newInstance(){
          return new YourFragment()
      }
}