在Android 7.1.1上的DatePicker中添加了额外的填充/边距
问题描述:
我试图创建一个显示DatePicker的自定义对话框,但是该元素在视图的右侧添加了额外的边距/填充.这仅在Android 7.1.1,Nexus 6P上发生,但在其他较低分辨率的手机上也可以正常工作.如何删除多余的填充物?我的XML布局代码:
I'm trying to make a custom dialog displaying the DatePicker, but the element adds an extra margin/padding to the right of the view. This happens only on Android 7.1.1, Nexus 6P, but works perfectly fine on other lower resolution phones. How do I remove the extra padding? My XML layout code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:endYear="2012"
android:startYear="1930" />
</LinearLayout>
在Android 7.1.1上看起来像这样:
It looks like this on Android 7.1.1:
添加了我的Java代码:
Added my Java code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_attributes);
assert getSupportActionBar() != null;
getSupportActionBar().setTitle("Register");
final TextView birthDateSelectedTextView = (TextView) findViewById(R.id.text_view_birth_date);
Button dialogOpenButton = (Button) findViewById(R.id.button_dialog_date_picker);
dialogOpenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(UserAttributesActivity.this);
final AlertDialog.Builder builder = new AlertDialog.Builder(UserAttributesActivity.this);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_date_picker, null);
builder.setView(dialogView);
final DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
day = datePicker.getDayOfMonth();
month = datePicker.getMonth() + 1;
year = datePicker.getYear();
String date = "" + day + "/" + month + "/" + year;
birthDateSelectedTextView.setText(date);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}
答
使用 AlertDialog.Builder
添加样式,如下所示.
Add Style with AlertDialog.Builder
like as follow.
AlertDialog.Builder builder = new AlertDialog.Builder(UserAttributesActivity.this, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar);