dmytro horozii
dmytro horozii

Reputation: 73

margin and layout in android

I want to create card like -

pictures,

I try to use margin, but not win with this idea. I try create for all element personal view and use padding or margin but I not have result.

I have code like that

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/rlCardBlock"
        style="@style/Widget.CardContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:maxLines="2"
            android:textStyle="bold"
            android:text="title"/>

        <TextView
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:maxLines="5"
            android:text="content" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@id/content"
            android:layout_marginBottom="8dp"
            android:background="@null"
            android:src="@drawable/ic_more_vert_black_36dp" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

Thank you for your attentions.

Upvotes: 1

Views: 13753

Answers (2)

nifCody
nifCody

Reputation: 2444

Android already have pre defined widgets for card and recycler view. You may use the android cardview and recycler view for your problem, it quiet easy for you to implement.

For this You first add dependencies if you are using android studio as IDE

compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'

then in your above metioned code change it into

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cv"
        >



<LinearLayout
android:id="@+id/rlCardBlock"
style="@style/Widget.CardContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:maxLines="2"
    android:textStyle="bold"
    android:text="title"/>

<TextView
    android:id="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:layout_marginTop="12dp"
    android:maxLines="5"
    android:text="content" />

 <TextView
    android:id="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/description"
    android:layout_marginTop="16dp"
    android:maxLines="5"
    android:text="content" />

<LinearLayout
android:id="@+id/rlCardBlock"
style="@style/Widget.CardContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal">

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Action 1"
  android:id="@+id/button1"
  />

 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Action 2"
  android:id="@+id/button2"
  />

</LinearLayout>

</LinearLayout>

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

</LinearLayout>

here is the tutorial : link

Upvotes: 1

khusrav
khusrav

Reputation: 5307

On title use android:layout_marginTop="22dp" On subtitle use android:layout_marginTop="12dp" On text use android:layout_margin="16dp"

But, you may choose other ways, either padding or margin

Upvotes: 1

Related Questions