gpt4 book ai didi

java - 自定义 DialogFragment 中的 Tablayout 不显示选项卡文本和图标

转载 作者:行者123 更新时间:2023-12-02 11:51:40 26 4
gpt4 key购买 nike

我将 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对话框。

最佳答案

试试这个

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

关于java - 自定义 DialogFragment 中的 Tablayout 不显示选项卡文本和图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47843377/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com