beatsspam
beatsspam

Reputation: 241

wrap_content or match_parent do not give the correct result

Friends, I'm trying to set height of view1 to the height of the filled text textView1, but it doesn’t work. view1 is thus infinitely long and exceeds the height of the text, occupies the entire screen, but I need to the end of the text.

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:weightSum="3"
        android:background="@android:color/white">
         <View
            android:id="@+id/view1"
            android:background="@drawable/rectangle_with_color"
            android:layout_height="wrap_content"
            android:layout_width="16dp" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="3dp"
            android:text="Не следует, однако забывать, что начало повседневной работы по формированию позиции представляет собой интересный эксперимент проверки новых предложений. С другой стороны реализация намеченных плановых заданий представляет собой интересный эксперимент проверки форм развития."
            android:gravity="top"
            />

    </RelativeLayout>
</androidx.cardview.widget.CardView>

rectangle_with_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#E41300" />
</shape>

enter image description here

Upvotes: 0

Views: 85

Answers (2)

Android Geek
Android Geek

Reputation: 9225

try this code :

 <?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="3"
        android:background="@android:color/white">
            <View
                android:id="@+id/view1"
                android:background="@drawable/rectangle_with_color"
                android:layout_height="wrap_content"
                android:layout_width="16dp"
                android:layout_alignBottom="@+id/textView1"/>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="3dp"
            android:text="Не следует, однако забывать, что начало повседневной работы по формированию позиции представляет собой интересный эксперимент проверки новых предложений. С другой стороны реализация намеченных плановых заданий представляет собой интересный эксперимент проверки форм развития."
            android:gravity="top" />

    </RelativeLayout>
</androidx.cardview.widget.CardView>

ScreenShot

Upvotes: 0

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2710

Fixed your bug. Please use this code below:

<View
            android:id="@+id/view1"
            android:background="@drawable/blank_checkbox"
            android:layout_height="wrap_content"
            android:layout_width="16dp"
            android:layout_alignBottom="@+id/textView1"/> //make this change

You need to set alignBottom property to mark its end.

As per documentation:

alignBottom: Makes the bottom edge of this view match the bottom edge of the given anchor view ID. Accommodates bottom margin.

Upvotes: 2

Related Questions