自定义DialogFragment中的Tablayout不显示选项卡文本和图标
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义DialogFragment中的Tablayout不显示选项卡文本和图标相关的知识,希望对你有一定的参考价值。
我把Tablayout
和ViewPager
放在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()来设置对话框样式,但它没有受到影响。
这是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" />
答案
试试这个
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);
以上是关于自定义DialogFragment中的Tablayout不显示选项卡文本和图标的主要内容,如果未能解决你的问题,请参考以下文章
在 DialogFragment 中为 AlertDialog 膨胀自定义视图时出现问题
Android自定义DialogFragment问题[重复]