为什么我的TextInputLayout的大纲框没有出现?

时间:2019-01-13 10:03:31

标签: android android-dialogfragment

我想用轮廓框包装我的编辑文本,所以我使用的是TextInputLayout,但是当我将对话框全屏显示时,轮廓框不会出现。

我检测到问题是因为我正在使用此行代码使(作业对话框)全屏显示:

   setStyle(STYLE_NO_TITLE,R.style.DialogHomework);

这是我的作业对话框xml:

  <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme"
android:fitsSystemWindows="true"
style="@android:style/Theme.NoTitleBar.Fullscreen"
xmlns:app="http://schemas.android.com/apk/res-auto">

<Button
    android:id="@+id/cancelHomework"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_marginStart="50dp"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="24dp"
    android:layout_marginEnd="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginBottom="24dp"
    android:background="@drawable/circle_button"
    android:text="Cancel"
    android:textColor="@color/colorPrimaryDark"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/addHomework" />

<android.support.design.widget.TextInputLayout

    android:id="@+id/hw_dialog_title_layout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/hw_dialog_title_margin_start"
    android:layout_marginLeft="@dimen/hw_dialog_title_margin_start"
    android:layout_marginTop="@dimen/hw_dialog_title_margin_top"
    android:layout_marginEnd="@dimen/hw_dialog_title_margin_end"
    android:layout_marginRight="@dimen/hw_dialog_title_margin_end"
    app:boxBackgroundColor="@android:color/transparent"
    app:boxBackgroundMode="outline"
    app:boxCornerRadiusBottomEnd="8dp"
    app:boxCornerRadiusBottomStart="8dp"
    app:boxCornerRadiusTopEnd="8dp"
    app:boxCornerRadiusTopStart="8dp"
    app:boxStrokeColor="@color/colorPrimaryDark"
    app:boxStrokeWidth="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/hw_dialog_material">

    <EditText
        android:id="@+id/hw_dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="@string/hw_dialog_title_hint"
        android:textColor="@color/colorPrimary" />
</android.support.design.widget.TextInputLayout>
<Spinner
    android:id="@+id/hw_dialog_material"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/hw_dialog_material_margin_start"
    android:layout_marginLeft="@dimen/hw_dialog_material_margin_start"
    android:layout_marginTop="@dimen/hw_dialog_material_margin_top"
    android:layout_marginEnd="@dimen/hw_dialog_material_margin_end"
    android:layout_marginRight="@dimen/hw_dialog_material_margin_end"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.TextInputLayout
    android:id="@+id/hw_dialog_homework_layout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="@dimen/hw_dialog_homework_margin_start"
    android:layout_marginLeft="@dimen/hw_dialog_homework_margin_start"
    android:layout_marginTop="@dimen/hw_dialog_homework_margin_top"
    android:layout_marginEnd="@dimen/hw_dialog_homework_margin_end"
    android:layout_marginRight="@dimen/hw_dialog_homework_margin_end"
    android:layout_marginBottom="@dimen/hw_dialog_homework_margin_bottom"
    android:gravity="top"
    app:boxBackgroundColor="@android:color/transparent"
    app:boxBackgroundMode="outline"
    app:boxCornerRadiusBottomEnd="8dp"
    app:boxCornerRadiusBottomStart="8dp"
    app:boxCornerRadiusTopEnd="8dp"
    app:boxCornerRadiusTopStart="8dp"
    app:boxStrokeColor="@color/primaryDark2"
    app:boxStrokeWidth="100dp"
    app:layout_constraintBottom_toTopOf="@+id/addHomework"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/hw_dialog_title_layout">

    <EditText
        android:id="@+id/hw_dialog_homework"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ems="10"
        android:gravity="top"
        android:hint="@string/homework_hint"
        android:inputType="textMultiLine" />
</android.support.design.widget.TextInputLayout>
<include

    android:id="@+id/toolBar"
    layout="@layout/toolbar"

    />

<Button
    android:id="@+id/addHomework"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_marginStart="50dp"
    android:layout_marginLeft="50dp"
    android:layout_marginEnd="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginBottom="50dp"
    android:background="@drawable/circle_button"
    android:text="@string/add"
    android:textColor="@color/colorPrimaryDark"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

我的作业对话框代码:

  public class newHomeWork extends DialogFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE,R.style.DialogHomework);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View dialog = inflater.inflate(R.layout.homework_dialog,container,false);
    Toolbar toolbar = dialog.findViewById(R.id.toolBar);
    toolbar.setTitleTextColor(getResources().getColor(R.color.colorPrimaryDark));
    TextView textView =toolbar.findViewById(R.id.homeworkDialogTitle);
    textView.setText("Add homework");
    Button add = dialog.findViewById(R.id.addHomework);
    Button cancel = dialog.findViewById(R.id.cancelHomework);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });
    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });


    return dialog;
}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

    Dialog dialog =  super.onCreateDialog(savedInstanceState);


    return dialog;
}

@Override
public void onStart() {
    super.onStart();

}
}

我的DialogHomeWork风格:

 <style name="DialogHomework" parent="Theme.AppCompat.Dialog">
    <item name="windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowFullscreen">false</item>
    <item name="android:windowBackground">@color/colorPrimary</item>
    <item name="android:background">@color/colorPrimary</item>


</style>

它的外观 enter image description here

我如何想要它: enter image description here

2 个答案:

答案 0 :(得分:0)

创建xml文件 round_rect_shape.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#ffffffff"/>    

<stroke android:width="3dp"
    android:color="#ff000000"
    />

<padding android:left="1dp"
     android:top="1dp"
     android:right="1dp"
     android:bottom="1dp"
     /> 

<corners android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp"
     android:topRightRadius="7dp"/> 
</shape>

然后使用它设置EditText的背景

<EditText
    android:background="@drawable/round_rect_shape"
    android:id="@+id/hw_dialog_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10"
    android:inputType="textPersonName"
    android:hint="@string/hw_dialog_title_hint"
    android:textColor="@color/colorPrimary" />

答案 1 :(得分:0)

您可以设置TextInputLayout的样式:

Widget.MaterialComponents.TextInputLayout.OutlinedBox

这应该做的事。

简单的例子:

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Test me">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>