Android 7.1.1上的DatePicker添加了额外的填充/边距

时间:2017-01-04 04:56:47

标签: android

我正在尝试创建一个显示DatePicker的自定义对话框,但该元素会在视图右侧添加额外的边距/填充。这种情况仅发生在Android 7.1.1,Nexus 6P上,但在其他低分辨率手机上运行得非常好。如何删除额外的填充? 我的XML布局代码:

<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上看起来像这样: Problem Screenshot

编辑:添加了我的Java代码:

 @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();
        }
    });
}

4 个答案:

答案 0 :(得分:9)

使用AlertDialog.Builder添加样式,如下所示。

AlertDialog.Builder builder = new AlertDialog.Builder(UserAttributesActivity.this, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar);

答案 1 :(得分:2)

@Ankita是对的,但是如果你正在使用AppCompat,那么事情会更复杂:

定义新样式并扩展AppCompat的Light.Dialog样式

<style name="DatePickerDialogStyle" parent="@style/Theme.AppCompat.Light.Dialog">
    ...
</style>

然后添加属性以模仿NoActionBar部分:

<style name="DatePickerDialogStyle" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

最后,既然你不再从你的AppTheme获取app compat颜色,请将它们复制到你的新主题:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>

<style name="DatePickerDialogStyle" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/primary<item>
    <item name="colorPrimaryDark">@color/primary_darkitem>
    <item name="colorAccent">@color/accent</item>
</style>

然后应用这个新主题:

AlertDialog.Builder builder 
    = new AlertDialog.Builder(UserAttributesActivity.this,
    DatePickerDialogStyle);

答案 2 :(得分:0)

使用此

DatePickerDialog.Builder builder = null;
// 5.0+ 需要更换主题以去除右侧白边
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    builder = new DatePickerDialog.Builder(this.getActivity(),
            android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar);
}
// 4.4 不需要,相反加上会有白边
else {
    builder = new DatePickerDialog.Builder(this.getActivity());
}

答案 3 :(得分:0)

如果您使用的是类似androidx.fragment.app.DialogFragment()的类。这样做:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Set the dialog style.
    setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar)
}