使用Base.Theme.AppCompat.Dialog
作为DialogFragment的主题时,我无法将填充应用于整个标题视图。我可以成功将paddingBottom
应用于标题文本,但会忽略该图标。
图标通过getDialog().setFeatureDrawable(Window.FEATURE_LEFT_ICON, drawable);
DialogFragment样式
<style name="my_dialog" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">@color/NavBarTint</item>
<item name="android:windowTitleStyle">@style/DialogWindowTitle</item>
<item name="android:windowNoTitle">false</item>
<!-- Doesn't apply to icon -->
<item name="drawablePadding">8dp</item>
<item name="colorAccent">@color/NavBarTint</item>
</style>
<style name="DialogWindowTitle">
<!-- Applies to only the text -->
<item name="android:paddingBottom">8dp</item>
<item name="android:textColor">@color/DialogTitleText</item>
<item name="android:textSize">22sp</item>
<item name="android:textColorPrimary">@color/DialogTitleText</item>
</style>
结果
我知道另一种方法是取消标题并制作自己的标题,但我们有很多对话都是从这种风格继承而来的,并且很想知道问题的根源而不是解决问题的方法。
由于
答案 0 :(得分:0)
抱歉,我不是英国人 dialogPreferredPadding attr dialogPreferredPadding Appcompat.Light.Dilaog 默认设置 @扪/ abc_dialog_padding_material 24dp
<style name="my_dialog" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">@color/NavBarTint</item>
<item name="android:windowTitleStyle">@style/DialogWindowTitle</item>
<item name="android:windowNoTitle">false</item>
<!-- Doesn't apply to icon -->
<item name="drawablePadding">8dp</item>
<item name="dialogPreferredPadding">0dp</item>
<item name="colorAccent">@color/NavBarTint</item>
</style>
答案 1 :(得分:0)
不得不将hack应用于棒棒糖后设备(最终将所有图标一起移除),但我会发布我们使用的内容。
/**
* This method can be called to add padding
* below an icon if used. New appcompat library introduced
* bug where padding below icon is ignored.
*/
public void applyAppCompatDialogIconPaddingFix() {
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) && (getView() instanceof ViewGroup)) {
ViewGroup rootView = (ViewGroup) getView();
if (rootView instanceof ScrollView) {
//going to safely assume a child exists...
rootView = (ViewGroup) rootView.getChildAt(0);
if (rootView == null) {
return;
}
}
float padding = ContextUtil.pxFromDp(getContext(), 12f);
View paddingFix = new View(getActivity());
paddingFix.setBackgroundResource(R.color.NavBarTint); //same color as title
if (rootView instanceof LinearLayout) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, (int) padding);
paddingFix.setLayoutParams(params);
} else if (rootView instanceof RelativeLayout) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, (int) padding);
paddingFix.setLayoutParams(params);
} else {
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, (int) padding);
paddingFix.setLayoutParams(params);
}
rootView.addView(paddingFix, 0);
}
}