自定义DialogFragment中的Tablayout未显示选项卡文本&图标

时间:2017-12-16 07:00:19

标签: java android tabs dialog android-tablayout

我将TablayoutViewPager放在DialogFragment中,因为我需要将Pager显示在对话框中。 DialogFragment显示此布局确定。但问题并没有显示文字和Tablayout的图标。

以下是DialogFragment的代码:

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;
    }
}

我也通过覆盖onCreate()来设置对话框样式,但它没有受到影响。

这是btoh Tablayout& DialogFragment:

<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>

这对我的Tablayout也没有影响。

<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 个答案:

答案 0 :(得分:1)

试试这个

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);