代码之家  ›  专栏  ›  技术社区  ›  Jayesh Rathod

自定义对话框片段中的Tablayout不同时显示选项卡文本和图标

  •  0
  • Jayesh Rathod  · 技术社区  · 7 年前

    我被放了 Tablayout 具有 ViewPager 在里面 DialogFragment 因为我需要在对话框中显示寻呼机。 对话框片段 显示此布局ok。但问题是不显示文本&的图标 表格布局 .

    这是的代码 对话框片段 :

    public class DialogFragmentWindow extends DialogFragment {
    
        TabLayout tabLayout;
        ViewPager vpFontStyle;
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
    
           setStyle(DialogFragment.STYLE_NORMAL, R.style.MY_DIALOG);
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
            WindowManager.LayoutParams lWindowParams = new WindowManager.LayoutParams();
            lWindowParams.copyFrom(getDialog().getWindow().getAttributes());
            lWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            lWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    
            getDialog().getWindow().setAttributes(lWindowParams);
    
            Window window = getDialog().getWindow();
            window.setGravity(Gravity.BOTTOM);
            window.requestFeature(Window.FEATURE_NO_TITLE);
    
            getDialog().setCancelable(true);
            getDialog().setCanceledOnTouchOutside(true);
    
            View view = inflater.inflate(R.layout.tab_layout_font_styles, container, true);
    
            tabLayout = (TabLayout) view.findViewById(R.id.tab_font_style);
            vpFontStyle = (ViewPager) view.findViewById(R.id.vp_font_style_tabs);
    
            tabLayout.addTab(tabLayout.newTab().setText("Style").setIcon(R.drawable.ic_font_size));
            tabLayout.addTab(tabLayout.newTab().setText("Family").setIcon(R.drawable.ic_font_type));    
            tabLayout.addTab(tabLayout.newTab().setText("Shadow").setIcon(R.drawable.ic_shadow));
    
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
            AdapterFragmentPager adapterFragmentPager = new AdapterFragmentPager(getChildFragmentManager(), tabLayout.getTabCount());
            vpFontStyle.setAdapter(adapterFragmentPager);
            tabLayout.setupWithViewPager(vpFontStyle);
            vpFontStyle.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                }
    
                @Override
                public void onPageSelected(int position) {
                    vpFontStyle.setCurrentItem(position, true);
                    tabLayout.getTabAt(position).select();
                }
    
                @Override
                public void onPageScrollStateChanged(int state) {
    
                }
            });
    
            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    vpFontStyle.setCurrentItem(tab.getPosition(), true);
                }
    
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
    
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
    
                }
            });
    
            return view;
        }
    }
    

    以下是btoh Tablayout&对话框片段:

    <style name="AppTabTextAppearance" parent="TextAppearance.Design.Tab">
        <item name="android:textSize">14dp</item>
        <item name="android:textColor">#000000</item>
        <item name="android:textStyle">bold</item>
        <item name="textAllCaps">false</item>
    </style>
    
    <style name="MY_DIALOG" parent="android:Theme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowIsFloating">false</item>
    
        <item name="android:textColor">#000000</item>
        <item name="colorAccent">#000000</item>
        <item name="android:textColorPrimary">#000000</item>
        <item name="android:textColorSecondary">#000000</item>
    </style>
    

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_font_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="#000000"
        app:tabIndicatorHeight="2dp"
        app:tabMode="fixed"
        app:tabSelectedTextColor="#000000"
        app:tabTextAppearance="@style/AppTabTextAppearance" />
    

    输出屏幕 image 对话框的。

    1 回复  |  直到 7 年前
        1
  •  1
  •   AskNilesh    7 年前

    试试这个

    AdapterFragmentPager adapterFragmentPager = new AdapterFragmentPager(getChildFragmentManager(), tabLayout.getTabCount());
    vpFontStyle.setAdapter(adapterFragmentPager);
    tabLayout.setupWithViewPager(vpFontStyle);   
    
    tabLayout.getTabAt(0).setText("Style");
    tabLayout.getTabAt(1).setText("Family");
    tabLayout.getTabAt(2).setText("Shadow");
    
    tabLayout.getTabAt(0).setIcon(R.drawable.ic_font_size);
    tabLayout.getTabAt(1).setIcon(R.drawable.ic_font_type);
    tabLayout.getTabAt(2).setIcon(R.drawable.ic_shadow);