具有角半径的Android对话框背景具有分层背景 [英] Android dialog background with corner radius has layered background
问题描述
我是Android的新手,也是Java的新手.在我的应用程序中,我弹出一个警报对话框,其中显示一个日期和时间选择器,供用户选择他们想要发送消息的日期时间.那部分进展顺利.
I'm new to Android, and really Java also. In my app, I've got an alert dialog I'm throwing up that displays a date and time picker for the user to select a datetime they would like to send a message. That part is going fine.
在我的xml中,当我设置一个background属性(当前为蓝色且具有拐角半径)时,似乎还有一个额外的白色背景,但没有在其后面窥见拐角半径.我有点沮丧.感谢您的帮助.
In my xml, when I set a background attribute, which is currently blue and has a corner radius, there is seemingly an additional white background without a corner radius peeking out behind it. I'm a little miffed. Any help is appreciated.
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>
corners_qk.xml:
corners_qk.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:radius="5dp" />
<solid
android:color="@color/qkblue"/>
</shape>
colors.xml:
colors.xml:
<resources>
<color name="red">#FF0000</color>
<color name="qkblue">#2483d0</color>
</resources>
java:
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();
推荐答案
实际上,您所看到的背景是AlertDialog
的default theme
,即Light_THEME
,没有办法使其透明.使用AlertDialog的方法就是使用Dialog,在其中可以使其主题透明.
Actually the the background that you are seeing is the default theme
of the AlertDialog
which is Light_THEME
, There is no way to make it transparent instead of using the AlertDialog just use the Dialog where you can make the its theme transparent.
示例:
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();
结果:
这篇关于具有角半径的Android对话框背景具有分层背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!