Erfan
Erfan

Reputation: 3353

layout_weight not work for CardView

read data from server and show that in card view . i do so many ways for give weight to card view but not work!

this is my code :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        card_view:cardCornerRadius="5dp">


           <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">


            <ImageView
                android:layout_gravity="center"
                android:id="@+id/img"
                android:scaleType="fitXY"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/tv_name"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:textSize="18sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/tv_version"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/tv_api_level"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </android.support.v7.widget.CardView>
    </LinearLayout>

and look like this :

enter image description here

but want my card view look like this :

enter image description here

how can i do this ? important is : do not want give height number like 50dp want give weight cause want in all devices look like pic2 . please help tnx

Upvotes: 1

Views: 1870

Answers (1)

Amit Vaghela
Amit Vaghela

Reputation: 22965

you need to use android:weightSum properly, giving android:layout_height="0dp" if you have android:orientation="vertical"

check below xml code,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:card_view="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:elevation="10dp"
            card_view:cardCornerRadius="5dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:orientation="vertical"
                android:weightSum="10">

                <ImageView
                    android:id="@+id/img1"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_gravity="center"
                    android:layout_weight="7"
                    android:scaleType="fitXY"
                    android:src="@mipmap/ic_true"/>

                <TextView
                    android:id="@+id/tv_name1"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_marginBottom="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="test"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/tv_version1"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="test"/>

                <TextView
                    android:id="@+id/tv_api_level1"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="test"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</LinearLayout>

Upvotes: 3

Related Questions