我的自定义对话框中有一个时间选择器。这是结果:
这是我的代码:
<style name="DialogStyler2" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">#FFFFFF</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowNoTitle">true</item>
</style>
final Dialog dialog = new Dialog(SabadKharid_s1.this, R.style.DialogStyler2);
dialog.setContentView(R.layout.dialogtime);
timePicker = (TimePicker) dialog.findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.show();
dialog.getWindow().setAttributes(lp);
如您所见,标题中的小时和分钟textView
为白色。如何更改背景颜色?
答案 0 :(得分:3)
如果您更改“背景”而不是“windowBackground”的颜色,则会发生这种情况。这对我有用:
styles.xml
<style name="DialogTheme" parent="Theme.AppCompat.Dialog">
<item name="android:windowBackground">#333</item>
<item name="colorAccent">#2BF</item>
</style>
MainActivity.java
Calendar cal = Calendar.getInstance();
TimePickerDialog tpd = new TimePickerDialog(
this,
R.style.DialogTheme,
this, // must implement TimePickerDialog.OnTimeSetListener
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
false);
tpd.show();
答案 1 :(得分:0)
背景不起作用,试试这个 -
<style name="DialogStyler2" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorAccent">@color/your_accent</item>
<item name="android:colorPrimary">@color/your_primary</item>
<item name="android:colorPrimaryDark">@color/your_primary_dark</item>
</style>
答案 2 :(得分:0)
您可以参考以下代码来获取时间戳样式。
MainActivity.java
import static android.R.attr.button;
public class MainActivity extends AppCompatActivity {
Calendar calendar ;
Button button;
TimePickerDialog timePickerDialog ;
int CalendarHour, CalendarMinute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
calendar = Calendar.getInstance();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CalendarHour = calendar.get(Calendar.HOUR_OF_DAY);
CalendarMinute = calendar.get(Calendar.MINUTE);
timePickerDialog = new TimePickerDialog(MainActivity.this, R.style.DialogTheme,new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
Toast.makeText(MainActivity.this, "Time Selected"+selectedHour+":"+selectedMinute, Toast.LENGTH_SHORT).show();
}
}, CalendarHour, CalendarMinute, true);//Yes 24 hour time
timePickerDialog.setTitle("Select Time");
timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
Toast.makeText(MainActivity.this, "Time Not Selected", Toast.LENGTH_SHORT).show();
}
});
timePickerDialog.show();
}
});
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK HERE TO SHOW TIME PICKER DIALOG WITH MATERIAL DESIGN THEME"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Style.xml
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/purple</item>
<item name="colorControlNormal">@color/orange</item>
</style>