Android 样式更改不适用于三星设备
Android style changes not working with Samsung devices
我想使用微调器样式全局更改 DatePickerDialogs 和 TimePickerDialogs。对于这种情况,我将 android:datePickerStyle 和 android:timePickerStyle 添加到我的 AppTheme。当我测试样式更改时,虚拟模拟 Nexus 设备上的一切都很好。但是当我在三星设备上测试它时,没有加载任何改变样式的地方。两种设备 运行 都带有牛轧糖。
这是我的 style.xml
的片段
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:datePickerStyle">@style/AppTheme.DatePicker</item>
<item name="android:timePickerStyle">@style/AppTheme.TimePicker</item>
</style>
<style name="AppTheme.DatePicker" parent="android:Widget.DatePicker">
<item name="android:spinnersShown">true</item>
<item name="android:calendarViewShown">false</item>
</style>
<style name="AppTheme.TimePicker" parent="android:Widget.Material.TimePicker">
<item name="android:timePickerMode">spinner</item>
</style>
这就是我调用 DatePickerDialog 的方式。 (TimePickerDialogs 的调用方式相同)
DatePickerDialog datePickerDialog = new DatePickerDialog(
getView().getContext(), SettingsFragment.this, 2000 , 10, 1);
datePickerDialog.show();
Settings.Fragment 是调用对话框的片段。
这是它应该的样子
Nexus device
这看起来不应该
Samsung device
编辑:
Nexus 设备是带有 Android 7.1.1 的虚拟模拟 Nexus 5X,Samsung 设备是带有 Android 7.0.
的 Samsung S7 Edge
我自己找到了解决该问题的方法。
解决方法是不使用全局样式。相反,我不得不编写自己的 DatePickerDialog。
在自定义对话框布局中,我可以使用我想要的 DatePicker 首选项。这看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<DatePicker
android:id="@+id/spinnerDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:focusable="true"
android:spinnersShown="true" />
</LinearLayout>
自定义对话框如下所示:
public class SpinnerDatePickerDialog extends DialogFragment {
private ISpinnerDatePickerDialogListener listener;
private DatePicker datePicker;
// this is no clean android fragment constructor, but we want to use it here
@SuppressLint("ValidFragment")
public SpinnerDatePickerDialog(ISpinnerDatePickerDialogListener listener) {
super();
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Build the dialog and set up the button click handlers
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);
datePicker = (DatePicker) view.findViewById(R.id.spinnerDatePicker);
datePicker.updateDate(defaultYear, defaultMonth, defaultDay);
builder.setView(view);
builder
.setPositiveButton(R.string.submit, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
listener.onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog.this);
}
})
.setNegativeButton(R.string.abort, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
listener.onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog.this);
}
});
return builder.create();
}
}
为了向原始片段发送值,我创建了一个接口。原始片段实现此接口,自定义对话框将片段作为其构造函数中的参数获取。这样我就可以触发原始片段中的侦听器。
界面:
public interface ISpinnerDatePickerDialogListener {
void onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog dialog);
void onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog dialog);
}
调用对话框的片段class:
public class SettingsFragment extends Fragment implements ISpinnerDatePickerDialogListener, ISpinnerTimePickerDialogListener {
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// initialize listeners for text inputs, to open picker dialogs
periodBegin = (EditText) getView().findViewById(R.id.editPeriodBegin);
periodBegin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(PERIOD_BEGIN_DIALOG_ID);
}
});
}
/**
* opens dialog for id
*
* @param id
*/
private void showDialog(int id) {
switch(id) {
case PERIOD_BEGIN_DIALOG_ID:
SpinnerDatePickerDialog datePickerDialog;
datePickerDialog = new SpinnerDatePickerDialog(this);
datePickerDialog.show(getFragmentManager(), datePickerDialog.getTAG());
break;
}
@Override
public void onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog dialog) {
// TODO send DatePicker values with listener
// load DatePicker from dialog and set them to EditText text
DatePicker datePicker = dialog.getDatePicker();
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();
// TODO
String formattedDate = FORMAT.format(new Date(year - 1900, month, day));
periodBegin.setText(formattedDate);
}
@Override
public void onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog dialog) {
// Nothing to do here
}
}
我想使用微调器样式全局更改 DatePickerDialogs 和 TimePickerDialogs。对于这种情况,我将 android:datePickerStyle 和 android:timePickerStyle 添加到我的 AppTheme。当我测试样式更改时,虚拟模拟 Nexus 设备上的一切都很好。但是当我在三星设备上测试它时,没有加载任何改变样式的地方。两种设备 运行 都带有牛轧糖。
这是我的 style.xml
的片段<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:datePickerStyle">@style/AppTheme.DatePicker</item>
<item name="android:timePickerStyle">@style/AppTheme.TimePicker</item>
</style>
<style name="AppTheme.DatePicker" parent="android:Widget.DatePicker">
<item name="android:spinnersShown">true</item>
<item name="android:calendarViewShown">false</item>
</style>
<style name="AppTheme.TimePicker" parent="android:Widget.Material.TimePicker">
<item name="android:timePickerMode">spinner</item>
</style>
这就是我调用 DatePickerDialog 的方式。 (TimePickerDialogs 的调用方式相同)
DatePickerDialog datePickerDialog = new DatePickerDialog(
getView().getContext(), SettingsFragment.this, 2000 , 10, 1);
datePickerDialog.show();
Settings.Fragment 是调用对话框的片段。
这是它应该的样子 Nexus device
这看起来不应该 Samsung device
编辑: Nexus 设备是带有 Android 7.1.1 的虚拟模拟 Nexus 5X,Samsung 设备是带有 Android 7.0.
的 Samsung S7 Edge我自己找到了解决该问题的方法。 解决方法是不使用全局样式。相反,我不得不编写自己的 DatePickerDialog。
在自定义对话框布局中,我可以使用我想要的 DatePicker 首选项。这看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<DatePicker
android:id="@+id/spinnerDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:focusable="true"
android:spinnersShown="true" />
</LinearLayout>
自定义对话框如下所示:
public class SpinnerDatePickerDialog extends DialogFragment {
private ISpinnerDatePickerDialogListener listener;
private DatePicker datePicker;
// this is no clean android fragment constructor, but we want to use it here
@SuppressLint("ValidFragment")
public SpinnerDatePickerDialog(ISpinnerDatePickerDialogListener listener) {
super();
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Build the dialog and set up the button click handlers
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);
datePicker = (DatePicker) view.findViewById(R.id.spinnerDatePicker);
datePicker.updateDate(defaultYear, defaultMonth, defaultDay);
builder.setView(view);
builder
.setPositiveButton(R.string.submit, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
listener.onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog.this);
}
})
.setNegativeButton(R.string.abort, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
listener.onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog.this);
}
});
return builder.create();
}
}
为了向原始片段发送值,我创建了一个接口。原始片段实现此接口,自定义对话框将片段作为其构造函数中的参数获取。这样我就可以触发原始片段中的侦听器。 界面:
public interface ISpinnerDatePickerDialogListener {
void onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog dialog);
void onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog dialog);
}
调用对话框的片段class:
public class SettingsFragment extends Fragment implements ISpinnerDatePickerDialogListener, ISpinnerTimePickerDialogListener {
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// initialize listeners for text inputs, to open picker dialogs
periodBegin = (EditText) getView().findViewById(R.id.editPeriodBegin);
periodBegin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(PERIOD_BEGIN_DIALOG_ID);
}
});
}
/**
* opens dialog for id
*
* @param id
*/
private void showDialog(int id) {
switch(id) {
case PERIOD_BEGIN_DIALOG_ID:
SpinnerDatePickerDialog datePickerDialog;
datePickerDialog = new SpinnerDatePickerDialog(this);
datePickerDialog.show(getFragmentManager(), datePickerDialog.getTAG());
break;
}
@Override
public void onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog dialog) {
// TODO send DatePicker values with listener
// load DatePicker from dialog and set them to EditText text
DatePicker datePicker = dialog.getDatePicker();
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();
// TODO
String formattedDate = FORMAT.format(new Date(year - 1900, month, day));
periodBegin.setText(formattedDate);
}
@Override
public void onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog dialog) {
// Nothing to do here
}
}