ConstraintLayout行为异常

时间:2019-01-17 14:04:27

标签: android android-constraintlayout android-relativelayout

这是我的对话框

public class TestDialog extends DialogFragment {

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.test_dialog, container, false);
    }
}

当我将RelativeLayout用于如下设计时,

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

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp48"
        android:layout_alignParentTop="true"
        android:fontFamily="@font/trebuchet"
        android:gravity="center"
        android:text="Test Dialog"
        android:textColor="?attr/colorAccent"
        android:textSize="18sp" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textTitle"
        android:src="@drawable/ic_baseline_public_24px"
        android:tint="@color/black" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textTitle"
        android:layout_marginStart="5dp"
        android:layout_toEndOf="@+id/icon"
        android:fontFamily="@font/trebuchet"
        android:text="This is very long text, This is very long text, This is very long text" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/imageView_close"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/close"
        android:src="@drawable/ic_baseline_close_24dx"
        android:text="@string/cancel" />
</RelativeLayout>

我得到这个输出。

enter image description here

但是当我将ConstraintLayout用于如下设计时,

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

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="0dp"
        android:layout_height="@dimen/dp48"
        android:fontFamily="@font/trebuchet"
        android:gravity="center"
        android:text="Test Dialog"
        android:textColor="?attr/colorAccent"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textTitle"
        android:src="@drawable/ic_baseline_public_24px"
        android:tint="@color/black"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textTitle" />

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:fontFamily="@font/trebuchet"
        android:text="This is very long text, This is very long text, This is very long text"
        app:layout_constraintStart_toEndOf="@+id/icon"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textTitle" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/imageView_close"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/close"
        android:src="@drawable/ic_baseline_close_24dx"
        android:text="@string/cancel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text" />
</androidx.constraintlayout.widget.ConstraintLayout>

我得到以下输出。

enter image description here

我不知道为什么会有这种奇怪的行为。仅在对话框中,我得到此行为。

如果约束布局中缺少任何内容,请让我认识的朋友,我会纠正自己。

3 个答案:

答案 0 :(得分:1)

我认为问题是您的ConstraintLayout标记中的Public Function IsOpen(ByVal oPath As String) As Boolean Try Dim oWriter As New PdfWriter(oPath) oWriter.Close() Return False Catch ex As Exception Return True End Try End Function 。由于对话框没有固定的宽度,因此无法使用export() { { const myTable = document.getElementById('table'); const myClone = myTable.cloneNode(true); // Delete options unselected $($(myClone).find('select')).each((index, element) => { $(element).find('option').not(':selected').remove(); }); // Replace input for value $($(myClone).find('input')).each((index, element) => { $(element).replaceWith(element.value); }); const ws: XLSX.WorkSheet = XLSX.utils.table_to_sheet(myClone); const wb: XLSX.WorkBook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, 'Sheet1'); /* save to file */ XLSX.writeFile(wb, 'SheetJS.xlsx'); } } 。也许您可以设置固定宽度或最小宽度。

答案 1 :(得分:1)

您要做的就是将“主题”设置为对话框。

 @Override
    public int getTheme() {
        return R.style.dialog;
    }

样式

<style name="dialog" parent="android:Theme.Dialog">
        <item name="android:windowBackground">@drawable/radius</item>
        <item name="android:windowMinWidthMajor">80%</item>
        <item name="android:windowMinWidthMinor">85%</item>
        <item name="android:windowNoTitle">true</item>
    </style>

radius.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/white" />
    <corners android:radius="10dp" />
    <padding
        android:bottom="8dp"
        android:left="8dp"
        android:right="8dp"
        android:top="8dp" />
</shape>

答案 2 :(得分:0)

首先,它不是ConstraintLayout,而是实现Dialog本身的方式。这是一个建议,根据Dialog Documentation

  

Dialog类是对话框的基类,但应避免   直接实例化Dialog。而是,使用以下之一   子类:

     

AlertDialog
  该对话框可以显示标题,最多三个按钮,一个   可选项目列表或自定义布局。

     

DatePickerDialog or TimePickerDialog
  具有预定义UI的对话框,允许用户   选择日期或时间。

因此,基本上,您将使用setView api:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
    // Setting my layout here
    builder.setView(R.layout.my_layout);
    builder.setPositiveButton(/* My Methods */)
           .setNegativeButton(/* My Methods */);
    return builder.create();
}

Creating a Custom Layout documentation中进行阅读。如果您想进行更多自定义,请在values> styles.xml中创建一个自定义主题,该主题可以在整个应用程序中使用,也可以与构建器一起设置,例如:

new AlertDialog.Builder(requireActivity(), R.style.My_Alert_Dialog_Theme);