Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 约束不良行为_Android_Android Constraintlayout_Android Relativelayout - Fatal编程技术网

Android 约束不良行为

Android 约束不良行为,android,android-constraintlayout,android-relativelayout,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.lay

这是我的对话

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>
<?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>
我得到这个输出

但当我在我的设计中使用ConstraintLayout时,如下图所示

<?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>
<?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>
我得到以下输出

我不知道为什么会有这种奇怪的行为。只有在对话中,我才会有这种行为


如果我在constraint layout中遗漏了什么,请让我认识朋友,我会纠正自己

我认为问题出在您的ConstraintLayout标记中的android:layout\u width=match\u parent。由于对话框没有固定的宽度,因此使用match_parent将不起作用。也许你可以设置一个固定的宽度,或者最小宽度。

你所要做的就是为你的对话框设置一个主题

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

radius.xml

首先,它不是ConstraintLayout,而是如何实现对话框本身。这是一个建议,根据:

Dialog类是dialogs的基类,但是您应该避免 直接实例化对话框。相反,请使用以下方法之一 子类:

警报对话框 可以显示标题、最多三个按钮和 可选项目列表或自定义布局

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

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

请在中阅读。如果需要更多自定义,请在values>styles.xml中创建一个自定义主题,该主题可以在整个应用程序中使用,或者只需与生成器一起设置即可,如:

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

我不知道,但是如果你把你的代码放在一个空的活动中,它不会被拉伸,所以问题出在外部。也许RelativeLayout对这些组件的布局有不同的规则。我从来没有真正使用过它这也是一个很好的解决方案,但最好使用宽度/高度作为百分比,而不是固定它。所以,你是说我们不应该使用DialogFragment,对吗?不是直接,而是扩展它,然后覆盖onCreateDialog,然后返回AlertDialog的实例,如上图所示,或者按照链接获取更多信息。问题是关于DialogFragment的,您的评论是关于Dialog的。这不是一回事。
new AlertDialog.Builder(requireActivity(), R.style.My_Alert_Dialog_Theme);