【问题标题】:Can a custom view be used as a TabItem?自定义视图可以用作 TabItem 吗?
【发布时间】:2017-04-15 06:43:54
【问题描述】:

android中的TabLayout类为你提供了一个TabItem,可以让你指定一个文本和一个图标。 是否可以将自定义视图用作 TabItem?

我的标签看起来像这样

您可以看到,除了图标和文本标签之外,我还有一个通知符号(黄色圆圈内的数字)。我怎样才能制作这样的标签?

【问题讨论】:

    标签: android android-layout android-tablayout


    【解决方案1】:

    在某些情况下,我们可能希望为每个选项卡应用自定义 XML 布局,而不是默认选项卡视图。为此,在将滑动选项卡附加到寻呼机后,遍历所有 TabLayout.Tabs:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Get the ViewPager and set it's PagerAdapter so that it can display items
            ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
            SampleFragmentPagerAdapter pagerAdapter = 
                new SampleFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this);
            viewPager.setAdapter(pagerAdapter);
    
            // Give the TabLayout the ViewPager
            TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
            tabLayout.setupWithViewPager(viewPager);
    
            // Iterate over all tabs and set the custom view
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                TabLayout.Tab tab = tabLayout.getTabAt(i);
                tab.setCustomView(pagerAdapter.getTabView(i));
            }
        }
    
        //...
    } 
    

    接下来,我们将 getTabView(position) 方法添加到 SampleFragmentPagerAdapter 类中:

    public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
       private String tabTitles[] = new String[] { "Tab1", "Tab2" };
       private int[] imageResId = { R.drawable.ic_one, R.drawable.ic_two };
    
        public View getTabView(int position) {
            // Given you have a custom layout in `res/layout/custom_tab.xml` with a TextView and ImageView
            View v = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
            TextView tv = (TextView) v.findViewById(R.id.textView);
            tv.setText(tabTitles[position]);
            ImageView img = (ImageView) v.findViewById(R.id.imgView);
            img.setImageResource(imageResId[position]);
            return v;
        }
    
    } 
    

    使用它,您可以为适配器中的每个页面设置任何自定义选项卡内容。

    SOURCE

    【讨论】:

    • 当然!不敢相信我没有在 Tablayout.Tab 类中看到 setCustomView。谢谢
    • 但它不能在 TabChange 监听器上工作,以突出显示颜色的选定选项卡
    • 要更改标签文本颜色,您可以创建一个 customView 并覆盖 setSelected(boolean selected) 方法以相应地更改其自己的颜色。
    • @Asym 有没有办法在 xml 中做到这一点?
    • 它没有用,因为当你使用 setCustomeView 时,点击 item 时突出显示的颜色将被删除。
    【解决方案2】:

    您可以为每个选项卡项使用任何布局。首先像这样将 TabItems 添加到 TabLayout; (我的布局每个选项卡有 2 个文本视图和 1 个图像)

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent">
    
        <com.google.android.material.tabs.TabItem
            android:id="@+id/ti_payroll_tab_tab1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout="@layout/your_custom_layout" />
    
        <com.google.android.material.tabs.TabItem
            android:id="@+id/ti_payroll_tab_tab2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout="@layout/your_custom_layout" />
    </com.google.android.material.tabs.TabLayout>
    

    那么你必须在自定义布局中找到并设置视图。

    TabLayout.Tab tab1 = tabLayout.getTabAt(0);
    
    tvTab1Title = tab1.getCustomView().findViewById(R.id.txt_payroll_tab_title);
    tvTab1Value = tab1.getCustomView().findViewById(R.id.txt_payroll_tab_value);
    ivTab1 = tab1.getCustomView().findViewById(R.id.img_payroll_tab_image);
    

    【讨论】:

    • 如何动态添加这个自定义标签?
    【解决方案3】:

    尝试使用下面的代码

    private View mCustomView;
    private ImageView mImageViewCustom;
    private TextView mTextViewCustom;
    private int count = 0;
    
    public View getCustomTab() {
        mCustomView = LayoutInflater.from(NewHomePageActivity.this).inflate(R.layout.custom_tab, null);
        mImageViewCustom = (ImageView) mCustomView.findViewById(R.id.custom_tab_imageView);
        mTextViewCustom = (TextView) mCustomView.findViewById(R.id.custom_tab_textView_count);
        if (count > 0) {
            mTextViewCustom.setVisibility(View.VISIBLE);
            mTextViewCustom.setText(String.valueOf(count));
        } else {
            mTextViewCustom.setVisibility(View.GONE);
        }
        return mCustomView;
    }
    
    private void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(R.mipmap.ic_home_gray_48);
        tabLayout.getTabAt(1).setIcon(R.mipmap.ic_follow_gray_48);
        tabLayout.getTabAt(2).setIcon(R.mipmap.ic_follower_gray_48);
        tabLayout.getTabAt(3).setIcon(R.mipmap.ic_news_event_gray_48);
        tabLayout.getTabAt(4).setCustomView(getCustomTab());
        tabLayout.getTabAt(5).setIcon(R.mipmap.ic_menu_gray_48);
    }
    

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary">
    
        <ImageView
            android:id="@+id/custom_tab_imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/ic_bell_gray_48"
            android:contentDescription="@string/image_dsc" />
    
        <TextView
            android:id="@+id/custom_tab_textView_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/shape_circle"
            android:padding="2dp"
            android:text="1"
            android:textColor="@color/colorWhite"
            android:textSize="11sp" />
    
    </FrameLayout>
    

    【讨论】:

    • 如何通过 onTabSelected 方法调用访问 customView 中的视图以应用样式?
    【解决方案4】:

    关于这个主题的文档很差。我们可以使用标签的setCustomView 方法来设置自定义视图。以下是一个工作示例:

    tab_layout.xml

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/tab_height"
        android:background="@color/primary_dark" />
    

    custom_tab_item.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="@dimen/tab_height"
        android:orientation="horizontal"
        android:padding="@dimen/tab_padding">
    
        <ImageView
            android:id="@+id/tabIcon"
            android:layout_width="@dimen/tab_icon"
            android:layout_height="@dimen/tab_icon"
            android:layout_centerVertical="true"/>
    
        <TextView
            android:id="@+id/tabTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/tabIcon"
            android:textColor="@color/white" />
    
        <TextView
            android:id="@+id/tabSubTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tabTitle"
            android:layout_toEndOf="@+id/tabIcon"
            android:textColor="@color/white" />
    
    </RelativeLayout>
    

    MainActivity.kt

    TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position ->
                when (position) {
                    0 -> {
                        tab.setCustomView(R.layout.tab_item)
                        tab.customView?.findViewById<ImageView>(R.id.tabIcon)
                            ?.setImageResource(R.drawable.tab1)
                        tab.customView?.findViewById<TextView>(R.id.tabTitle)?.setText(R.string.tab1)
                    }
                    1 -> {
                        tab.setCustomView(R.layout.tab_item)
                        tab.customView?.findViewById<ImageView>(R.id.tabIcon)
                            ?.setImageResource(R.drawable.tab2)
                        tab.customView?.findViewById<TextView>(R.id.tabTitle)
                            ?.setText(R.string.tab2)
                    }
                }
            }.attach()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多