如何使片段适合所有屏幕尺寸的FrameLayout?

时间:2019-03-27 19:06:44

标签: android xml android-layout android-fragments

我正在开发一个应用,希望在容器中依次显示几个片段(框架布局)。我希望我的片段适合框架布局,而其部分不会超出屏幕。

我想要这个:

I want this:

但是得到这个:

But I get this:

这是我的代码段:

<android.support.constraint.ConstraintLayout
    android:id="@+id/relativeLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="24dp"
    android:layout_marginEnd="24dp"
    android:layout_marginBottom="24dp"
    android:background="@drawable/grey_border_circular_rect"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline3"
    app:layout_constraintVertical_bias="0.0">


     <FrameLayout
        android:id="@+id/fragment_holder"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="32dp"
        app:layout_constraintBottom_toTopOf="@+id/elipse1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>

    <android.support.constraint.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_end="32dp" />


    <ImageView
        android:id="@+id/elipse1"
        android:layout_width="14dp"
        android:layout_height="14dp"
        app:layout_constraintEnd_toStartOf="@+id/elipse2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline4"
        app:srcCompat="@drawable/ic_elipse_filled"
        tools:layout_editor_absoluteY="568dp" />

    <ImageView
        android:id="@+id/elipse2"
        android:layout_width="14dp"
        android:layout_height="14dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/elipse1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/elipse1"
        app:srcCompat="@drawable/ic_elipse_blank" />
</android.support.constraint.ConstraintLayout>
  

我无法共享完整的xml代码,因为我对代码有一些限制。但是Root布局也是ConstraintLayout。

fragment.xml

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/otpVerificationTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="OTP Verification"
    android:textColor="@color/blue"
    android:textSize="24sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<View
    android:id="@+id/view2"
    android:layout_width="wrap_content"
    android:layout_height="1dp"
    android:layout_marginTop="8dp"
    android:background="@color/btn_grey"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/otpVerificationTV" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="40dp"
    android:layout_marginEnd="8dp"
    android:text="We sent you a code"
    android:textColor="@android:color/black"
    android:textSize="26sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/view2" />

<Button
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="56dp"
    android:background="@color/transparent"
    android:text="Resend OTP"
    android:textAllCaps="false"
    android:textColor="@color/grey_secondary"
    android:textSize="20sp"
    app:layout_constraintEnd_toStartOf="@+id/view3"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView6" />

<View
    android:id="@+id/view3"
    android:layout_width="3dp"
    android:layout_height="30dp"
    android:layout_marginStart="10dp"
    android:background="@color/grey_secondary"
    app:layout_constraintBottom_toBottomOf="@+id/textView7"
    app:layout_constraintEnd_toStartOf="@+id/textView7"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/textView5" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:textColor="@color/blue"
    android:textSize="20sp"
    android:textStyle="bold"
    app:layout_constraintBaseline_toBaselineOf="@+id/textView5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/view3"
    tools:text="29 sec" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="24dp"
    android:layout_marginTop="56dp"
    android:text="OTP"
    android:textColor="@android:color/black"
    android:textSize="26sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView5" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="262dp"
    android:layout_height="48dp"
    android:layout_marginTop="8dp"
    android:background="@color/blue"
    android:orientation="vertical"
    app:layout_constraintStart_toStartOf="@+id/textView8"
    app:layout_constraintTop_toBottomOf="@+id/textView8" />

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="72dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:background="@drawable/blue_circular_rect"
    android:elevation="@dimen/btn_elevation"
    android:text="Next"
    android:textAllCaps="false"
    android:textColor="@color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/linearLayout"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
</android.support.constraint.ConstraintLayout>

那么我有什么可能的方法可以使片段适合任何屏幕尺寸,并且所有片段都可以在帧布局内看到。

1 个答案:

答案 0 :(得分:0)

以下代码将帮助您处理 fragment.xml 文件。 您没有提供更多的值文件,包括颜色代码,字符串文件等。因此,以下是文件中的一些更改,例如可绘制和与值相关的数据。

如果您对以下观点有任何疑问,请随时发表评论。如果以下代码适合您,请批准此答案。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/otpVerificationTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="OTP Verification"
        android:textColor="@android:color/holo_blue_light"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorGrey"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/otpVerificationTV" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="We sent you a code"
        android:textColor="@android:color/black"
        android:textSize="26sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/group2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/group2" />

    <android.support.constraint.Group
        android:id="@+id/group2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:constraint_referenced_ids="textView6"
        app:layout_constraintBottom_toTopOf="@id/group"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/view2" />

    <android.support.constraint.Group
        android:id="@+id/group"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:constraint_referenced_ids="view3,textView7,textView5"
        app:layout_constraintBottom_toTopOf="@id/group1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/group2" />

    <android.support.constraint.Group
        android:id="@+id/group1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:constraint_referenced_ids="linearLayout,textView8"
        app:layout_constraintBottom_toTopOf="@id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/group" />

    <Button
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:text="Resend OTP"
        android:textAllCaps="false"
        android:textColor="@color/colorGrey"
        android:textSize="20sp"
        app:layout_constraintEnd_toStartOf="@+id/view3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="@id/group"
        app:layout_constraintTop_toTopOf="@+id/group" />

    <View
        android:id="@+id/view3"
        android:layout_width="3dp"
        android:layout_height="30dp"
        android:layout_marginStart="10dp"
        android:background="@color/colorGrey"
        app:layout_constraintBottom_toBottomOf="@+id/textView7"
        app:layout_constraintEnd_toStartOf="@+id/textView7"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView5" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:textColor="@android:color/holo_blue_light"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView5"
        app:layout_constraintEnd_toEndOf="@id/group"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view3"
        tools:text="29 sec" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:text="OTP"
        android:textColor="@android:color/black"
        android:textSize="26sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/group1" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="262dp"
        android:layout_height="48dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/holo_blue_light"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="@+id/textView8"
        app:layout_constraintTop_toBottomOf="@+id/textView8" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:background="@android:color/holo_blue_light"
        android:elevation="1dp"
        android:text="Next"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/linearLayout"
        app:layout_constraintTop_toBottomOf="@+id/group1" />
</android.support.constraint.ConstraintLayout>