kyl*_*yle 1 android android-layout
我是 Android 新手,也是 Java 新手。在我的应用程序中,我有一个我正在抛出的警报对话框,它显示了一个日期和时间选择器,供用户选择他们想要发送消息的日期时间。那部分进展顺利。
在我的 xml 中,当我设置当前为蓝色并具有圆角半径的背景属性时,似乎有一个额外的白色背景,而没有圆角半径在其后面窥视。我有点恼火。任何帮助表示赞赏。
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/corners_qk"
android:layout_width="match_parent"
android:padding="8dp"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:calendarViewShown="true"
android:spinnersShown="false"
android:layout_weight="4"
android:layout_height="0dp" />
<TimePicker
android:id="@+id/time_picker"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="0dp" />
<Button
android:id="@+id/date_time_set"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="@drawable/corners"
android:text="@string/picker_button"
android:layout_height="0dp" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
角点_qk.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:radius="5dp" />
<solid
android:color="@color/qkblue"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
颜色.xml:
<resources>
<color name="red">#FF0000</color>
<color name="qkblue">#2483d0</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
爪哇:
final View dialogView = View.inflate(this, R.layout.date_time_picker, null);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
long time = calendar.getTimeInMillis();
try {
messageSend();
} catch (JSONException | IOException e) {
e.printStackTrace();
}
}});
alertDialog.setView(dialogView);
alertDialog.show();
Run Code Online (Sandbox Code Playgroud)
其实,你所看到的背景是default theme
的AlertDialog
是Light_THEME
,有没有办法让它透明,而不是使用AlertDialog只需使用对话框,你可以做它的主题是透明的。
例子:
final Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.date_time_picker);
alertDialog.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePicker datePicker = (DatePicker) alertDialog.findViewById(R.id.date_picker);
TimePicker timePicker = (TimePicker) alertDialog.findViewById(R.id.time_picker);
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
long time = calendar.getTimeInMillis();
}});
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
alertDialog.show();
Run Code Online (Sandbox Code Playgroud)
结果: