如何使用两个NumberPicker和TextView将片段对话框布局居中

时间:2017-05-20 18:42:28

标签: android android-layout fragment

我正在玩Android应用程序。使用ConstraintLayout和可视化编辑器标记主Activity没有问题。但看起来它与DialogFragment的工作方式不同。我有以下DialogFragment代码

public class AmountPickerFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.amount_picker_dialog, null);
    NumberPicker amountPicker = (NumberPicker) view.findViewById(R.id.amountPicker);
    amountPicker.setMinValue(1);
    amountPicker.setMaxValue(100);

    NumberPicker amountPickerTotal = (NumberPicker) view.findViewById(R.id.amountPickerTotal);
    amountPickerTotal.setMinValue(1);
    amountPickerTotal.setMaxValue(100);

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(view)
            // Add action buttons
            .setPositiveButton(R.string.amount_picker_ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // sign in the user ...
                }
            })
            .setNegativeButton(R.string.amount_picker_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    AmountPickerFragment.this.getDialog().cancel();
                }
            });
    return builder.create();
}

这是我的布局标记

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/amountPickerLaout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<NumberPicker
    android:id="@+id/amountPicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="of" />

<NumberPicker
    android:id="@+id/amountPickerTotal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

输出是 enter image description here

我想要类似的东西 enter image description here

请建议我如何实现这种对话框布局。

3 个答案:

答案 0 :(得分:1)

你能试试吗?

pandas.read_table

答案 1 :(得分:0)

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/amountPickerLaout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">

<NumberPicker
    android:id="@+id/amountPicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:text="of" />

<NumberPicker
    android:id="@+id/amountPickerTotal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

</LinearLayout>

答案 2 :(得分:0)

好吧,看看这个,你可以非常轻松地调整它,以便根据需要调整视图

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <NumberPicker
        android:id="@+id/amountPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="32dp"
        android:layout_alignRight="@+id/textView"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="of"/>

    <NumberPicker
        android:id="@+id/amountPickerTotal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="32dp"
        android:layout_alignLeft="@+id/textView"/>
</RelativeLayout>