简体   繁体   English

将自定义视图添加到选项卡

[英]Add custom view to tab

I want to add custom view for my tabs.I researched the internet but I can't find good tutorial.I am adding tabs with this code in onCreate event. 我想为标签页添加自定义视图。我研究了互联网,但找不到很好的教程。我在onCreate事件中使用此代码添加标签页。

        private String[] tabs = { "Tab 1", "Tab 2"};
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

I don't have any xml file for tabs. 我没有用于标签的xml文件。

How can I do this ? 我怎样才能做到这一点 ? Can you give an example ? 能给我举个例子吗 ?

first of all i suggest you use : 首先,我建议您使用:

https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html

and do not use actionbar tabs. 并且不要使用操作栏标签。

that is a source code of tabs of google play app. 这是google play应用程序标签的源代码。 after that you can create your own layout use mSlidingTabLayout.setCustomTabView(int layoutResId, int textViewId) to inflate a custom layout for the SlidingTabLayout tab views. 之后,您可以使用mSlidingTabLayout.setCustomTabView(int layoutResId, int textViewId)创建自己的布局,以为SlidingTabLayout选项卡视图添加自定义布局。 look at: 看着:

Android SlidingTabLayout with icons 带有图标的Android SlidingTabLayout

for complete example. 为完整的例子。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">

    <com.example.android.common.view.SlidingTabLayout
          android:id="@+id/sliding_tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
          android:id="@+id/viewpager"
          android:layout_width="match_parent"
          android:layout_height="0px"
          android:layout_weight="1"
          android:background="@android:color/white"/>

</LinearLayout>

and in your fragment or activity : 并且在您的fragmentactivity

mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new MyAdapter());
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);

your adapter of mViewPager must override below method 您的mViewPager适配器必须覆盖以下方法

 @Override
        public CharSequence getPageTitle(int position) {
            return your tab title;
        }

UPDATE: 更新:

first step is creating custom layout for each tabs so: 第一步是为每个选项卡创建自定义布局,以便:

     <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
/>

then when you want to add tabs to actionbar you must do : 然后,当您要将标签添加到操作栏时,必须执行以下操作:

    ActionBar.Tab tab1=actionBar.newTab();
    tab1.setTabListener(this);
    tab1.setCustomView(R.layout.tab);
    TextView txt1 = (TextView)tab1.getCustomView().findViewById(R.id.text1);
    txt1.setText("Tab 1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM