Center icon to the available space on Android

1

I want to create a layout for the elements of grid in a regilla way, its base is an icon, title and total

Now what I have is more or less like the following image

The layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="120dp"
    android:layout_height="120dp"
    android:orientation="horizontal"
    android:padding="5dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            app:srcCompat="@drawable/ic_menu_manage" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:paddingTop="4dp"
            android:paddingBottom="4dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:orientation="vertical"
            android:layout_alignParentBottom="true">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start"
                tools:text="Favorites" />

            <TextView
                android:id="@+id/size"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start"
                tools:text="12" />
        </LinearLayout>

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

What does not come out

The icon is now centered on the whole of the box, so I need the icon to focus only on what the white space occupies.

    
asked by Webserveis 08.09.2017 в 15:02
source

1 answer

1

I have achieved the end by adjusting the spaces with android:layout_weight

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

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="2"
            app:srcCompat="@drawable/ic_menu_manage" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:background="@color/colorAccent"
            android:orientation="vertical"
            android:paddingBottom="4dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:paddingTop="4dp">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start"
                tools:text="Favorites" />

            <TextView
                android:id="@+id/summary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start"
                tools:text="12" />
        </LinearLayout>

    </LinearLayout>
</android.support.v7.widget.CardView>
    
answered by 08.09.2017 в 18:59