在DialogFragment中显示ConstraintLayout

24

希望你能帮我解决一个问题。我已经尝试在Google上搜索并查找这里的主题,但没有找到答案。

最近我将我的布局更改为ConstraintLayouts,目前一切都很顺利。然而,我的问题是我有一些显示LinearLayoutDialogFragments,当我将其更改为ConstraintLayout时,它只显示了一个褪色的覆盖层,但没有布局中的任何元素。如果我将宽度和高度硬编码为100dp之类的值,则会显示该区域。当我尝试使用match_parent或任何其他组合时,仍然存在相同的问题。我还尝试将高度和宽度设置为设备高度和宽度,但仍然没有运气。

在我的CreateWeddingMetadataFragment中的onCreateView方法中:

createButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "Show message dialog");

            FragmentManager fm = getFragmentManager();
            CreateWeddingMetadataDialogFragment welcomeMessageDialog = new CreateWeddingMetadataDialogFragment();
            welcomeMessageDialog.show(fm, "metadate_fragment_dialog");
            welcomeMessageDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                }
            });

在我的CreateWeddingMetadataFragmentDialog中,我有:

public class CreateWeddingMetadataDialogFragment extends DialogFragment{

private static final String TAG = CreateWeddingMetadataDialogFragment.class.getSimpleName();

public Button setupNow, setupLater;
public TextView mTest;
public ConstraintLayout parentLayout;

private DialogInterface.OnDismissListener onDismissListener;



public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
    this.onDismissListener = onDismissListener;
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    if (onDismissListener != null) {
        onDismissListener.onDismiss(dialog);
    }
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "I was called");
}

public CreateWeddingMetadataDialogFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.create_welcome_message_view, container);

    setupNow = (Button) view.findViewById(R.id.setupNowButton);
    setupLater = (Button) view.findViewById(R.id.setupLaterButton);
    mTest = (TextView) view.findViewById(R.id.welcomeMessageTitle);
    parentLayout = (ConstraintLayout) view.findViewById(R.id.clWelcomeMessage);

    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    int width = displayMetrics.widthPixels;
    int height = displayMetrics.heightPixels;

    parentLayout.setMinimumWidth(width);
    parentLayout.setMinimumHeight(height);

    mTest.setText("Test");
    Log.d(TAG, "I was called onCreateView" + mTest);

    setupNow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Go to metadate page (Hide this dialog)
        }
    });

    setupLater.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Go to signin page and create the wedding
        }
    });


    return view;
}

这是我尝试将其设置为设备大小的结果。

下面是create_welcome_message_view函数:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent"
                                         xmlns:tools="http://schemas.android.com/tools"
                                         android:orientation="vertical"
                                         android:id="@+id/clWelcomeMessage">



<Button
    android:text="DET GØR JEG SENERE"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/setupLaterButton"
    app:layout_constraintTop_toTopOf="@+id/guideline19"
    app:layout_constraintBottom_toTopOf="@+id/guideline20"
    android:layout_marginEnd="16dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="16dp"
    android:background="@drawable/border"
    android:layout_marginTop="2dp"
    android:textColor="@color/colorPrimaryDark"/>

<Button
    android:text="JEG VIL OPSÆTTE DEN NU"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/setupNowButton"
    app:layout_constraintTop_toTopOf="@+id/guideline12"
    app:layout_constraintBottom_toTopOf="@+id/guideline19"
    android:layout_marginEnd="16dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="16dp"
    android:background="@color/colorPrimaryDark"
    android:layout_marginBottom="2dp"
    android:textColor="@color/White"/>

<TextView
    android:text="EN PERSONLIG VELKOMST"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/welcomeMessageTitle"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginEnd="32dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="32dp"
    android:layout_marginStart="32dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="32dp"
    app:layout_constraintBottom_toTopOf="@+id/textView5"
    android:layout_marginBottom="8dp"
    android:textAlignment="center"
    android:textSize="16sp"/>

<TextView
    android:text="Nu skal I definere den velkomst, som jeres gæster får i appen. I kan skrive navnet på brylluppet, en hlsen og har mulighed at vælge et coverbillede"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/textView5"
    app:layout_constraintTop_toTopOf="@+id/guideline14"
    android:layout_marginEnd="32dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="32dp"
    app:layout_constraintBottom_toTopOf="@+id/textView6"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="32dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="32dp"
    android:textAlignment="center"/>

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline12"
    android:orientation="horizontal"
    tools:layout_editor_absoluteY="204dp"
    tools:layout_editor_absoluteX="0dp"
    app:layout_constraintGuide_percent="0.4"/>

<TextView
    android:text="Du vil altid kunne ændre din opsætning af velkomsten i din brugerprofil."
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/textView6"
    app:layout_constraintBottom_toTopOf="@+id/guideline12"
    android:layout_marginBottom="8dp"
    app:layout_constraintTop_toTopOf="@+id/guideline15"
    android:layout_marginStart="32dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="32dp"
    android:layout_marginEnd="32dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="32dp"
    android:textAlignment="center"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintVertical_bias="1.0"/>

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline14"
    android:orientation="horizontal"
    tools:layout_editor_absoluteY="51dp"
    tools:layout_editor_absoluteX="0dp"
    app:layout_constraintGuide_percent="0.1"/>

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline15"
    android:orientation="horizontal"
    tools:layout_editor_absoluteY="153dp"
    tools:layout_editor_absoluteX="0dp"
    app:layout_constraintGuide_percent="0.3"/>

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline19"
    android:orientation="horizontal"
    tools:layout_editor_absoluteY="255dp"
    tools:layout_editor_absoluteX="0dp"
    app:layout_constraintGuide_percent="0.5"/>

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline20"
    android:orientation="horizontal"
    tools:layout_editor_absoluteY="305dp"
    tools:layout_editor_absoluteX="0dp"
    app:layout_constraintGuide_percent="0.6"/>

</android.support.constraint.ConstraintLayout>

如果您需要任何额外的信息,请告诉我,提前感谢。


我相信这是 ConstraintLayout 的一个 bug。你可以将你的布局包裹在 FrameLayout 中,并为 ConstraintLayout 设置固定大小 - 视图会出现。 - Outofdate
1个回答

4

这个很好 - 请看

1) 在XML中,如果可能的话最好按照可视化的顺序排列项目,以便以后容易理解。 2) 看起来每个元素都受到其他元素高度和宽度的限制,大多数情况下会导致0高度和宽度。注意LayoutParams上的0dp和wrap_content。 3) 也可以使用Vertical Bias的支持。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/clWelcomeMessage"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.constraint.Guideline
    android:id="@+id/guideline_ver_16"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="@dimen/sixteenDP"/>
<android.support.constraint.Guideline
    android:id="@+id/guideline_ver_end_16"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_end="@dimen/sixteenDP"/>

<TextView
  android:id="@+id/welcomeMessageTitle"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginBottom="8dp"
  android:layout_marginEnd="32dp"
  android:layout_marginLeft="32dp"
  android:layout_marginRight="32dp"
  android:layout_marginStart="32dp"
  android:layout_marginTop="16dp"
  android:text="EN PERSONLIG VELKOMST"
  android:textAlignment="center"
  android:textSize="16sp"
  app:layout_constraintBottom_toTopOf="@+id/textView5"
  app:layout_constraintHorizontal_bias="0.5"
  app:layout_constraintLeft_toLeftOf="parent"
  app:layout_constraintRight_toRightOf="parent"
  app:layout_constraintTop_toTopOf="parent"/>

<android.support.constraint.Guideline
 android:id="@+id/guideline1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 app:layout_constraintGuide_percent="0.1"/>
<android.support.constraint.Guideline
 android:id="@+id/guideline2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 app:layout_constraintGuide_percent="0.2"/>

<TextView
 android:id="@+id/textView5"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_marginBottom="8dp"
 android:text="Nu skal I definere den velkomst, som jeres gæster får i 
 appen. 
 I kan skrive navnet på brylluppet, en hlsen og har mulighed at vælge et 
 coverbillede"
 android:textAlignment="center"
 app:layout_constraintEnd_toEndOf="@+id/guideline_ver_end_16"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toStartOf="@+id/guideline_ver_16"
 app:layout_constraintTop_toBottomOf="@+id/guideline2"/>

<TextView
 android:id="@+id/textView6"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_marginBottom="8dp"
 android:layout_marginEnd="16dp"
 android:layout_marginLeft="16dp"
 android:layout_marginRight="16dp"
 android:layout_marginStart="16dp"
 android:layout_marginTop="8dp"
 android:text="Du vil altid kunne ændre din opsætning af velkomsten i din 
 brugerprofil."
 android:textAlignment="center"
 app:layout_constraintBottom_toTopOf="@+id/setupNowButton"
 app:layout_constraintHorizontal_bias="0.0"
 app:layout_constraintLeft_toLeftOf="@+id/guideline_ver_16"
 app:layout_constraintRight_toRightOf="@+id/guideline_ver_end_16"
 app:layout_constraintTop_toBottomOf="@+id/textView5"/> 

<Button
 android:id="@+id/setupNowButton"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_marginBottom="8dp"
 android:layout_marginEnd="16dp"
 android:layout_marginLeft="16dp"
 android:layout_marginRight="16dp"
 android:layout_marginStart="16dp"
 android:layout_marginTop="8dp"
 android:background="@color/colorPrimaryDark"
 android:text="JEG VIL OPSÆTTE DEN NU"
 android:textColor="@color/White"
 app:layout_constraintBottom_toTopOf="@+id/setupLaterButton"
 app:layout_constraintHorizontal_bias="0.0"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/textView6"
 app:layout_constraintVertical_bias="1.0"/>


<android.support.constraint.Guideline
 android:id="@+id/guideline6"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 app:layout_constraintGuide_percent="0.6"/>

<Button
    android:id="@+id/setupLaterButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:text="DET GØR JEG SENERE"
    android:background="@drawable/border"
    android:textColor="@color/colorPrimaryDark"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline6"
    app:layout_constraintVertical_bias="1.0"/>

 </android.support.constraint.ConstraintLayout>

此外,还有app:layout_constraintWidth_default="wrap"(宽度设置为0dp的情况)。如果设定了此属性,控件将具有与使用wrap_content相同的大小,但受到约束的限制(即它不会超出这些限制而扩展)。 支持资源

嗨,感谢您的回答。那时候,这是约束布局当前版本中的一个错误。 - Jesper Purup

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接