Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

android - How to create a view based on its content in constraint layout?

I have a view which needs to be shown at the bottom and also view height should be based on the content (the view's linked with parent). If I link the top constraint of error_view with parent, then its entire space is used.

what is the use of constraintLayout inside constraintLayout and when we should go? Is this solution match for my problem?

enter image description here

<androidx.constraintlayout.widget.ConstraintLayout ...>
     <View
        android:id="@+id/error_view"
        android:layout_width="0dp"
        android:layout_height="160dp"  // How to keep height based on content rather than defined height??
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:layout_marginBottom="40dp"
        android:background="@drawable/display_result"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/textureView"
       />

    <TextView
        android:id="@+id/error_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="12dp"
        android:layout_marginBottom="12dp"
        android:fontFamily="@font/poppins_medium"
        android:text="@string/error_msg"
        android:textAlignment="center"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/error_try_again"
        app:layout_constraintEnd_toEndOf="@+id/error_view"
        app:layout_constraintStart_toStartOf="@+id/error_view"
        app:layout_constraintTop_toTopOf="@+id/error_view" />

    <Button
        android:id="@+id/error_try_again"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:layout_marginBottom="24dp"
        android:background="@drawable/primary_button"
        android:text="@string/try_again"
        android:textAlignment="center"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/error_view"
        app:layout_constraintEnd_toEndOf="@+id/error_view"
        app:layout_constraintStart_toStartOf="@+id/error_view" />
</androidx.constraintlayout.widget.ConstraintLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use nested constraint layout or you can use any other ViewGroup like RelativeLayout, LinearLayout etc according to your needs.

In your case, As you View height should be according to the content so you can not fix it height. Suggested solutions is:

<androidx.constraintlayout.widget.ConstraintLayout ...>
    <androidx.constraintlayout.widget.ConstraintLayout
           android:layout_marginStart="12dp"
            android:layout_marginEnd="12dp"
            android:layout_marginBottom="40dp"
            android:background="@drawable/display_result">
          <TextView
            android:id="@+id/error_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="12dp"
            android:layout_marginBottom="12dp"
            android:fontFamily="@font/poppins_medium"
            android:text="@string/error_msg"
            android:textAlignment="center"
            android:textColor="@color/white"
            app:layout_constraintBottom_toTopOf="@+id/error_try_again"
            app:layout_constraintEnd_toEndOf="@+id/error_view"
            app:layout_constraintStart_toStartOf="@+id/error_view"
            app:layout_constraintTop_toTopOf="@+id/error_view" />
    
        <Button
            android:id="@+id/error_try_again"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginEnd="12dp"
            android:layout_marginBottom="24dp"
            android:background="@drawable/primary_button"
            android:text="@string/try_again"
            android:textAlignment="center"
            android:textColor="#FFFFFF"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/error_view"
            app:layout_constraintEnd_toEndOf="@+id/error_view"
            app:layout_constraintStart_toStartOf="@+id/error_view" />
    </constraint>
</androidx.constraintlayout.widget.ConstraintLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...