1

It seems that the the Android layout_width and layout_height only includes the content and the padding of the view. If so, why does the following code work?

I have read this post, however, although I understand what needed to be done in that case, I don't understand why it works.

I am using RecyclerView, with similar code to the linked example. Each item of the RecyclerView is a CardView

<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:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent "
android:layout_height="200dp"
card_view:cardCornerRadius="4dp"
android:padding="40dp"
android:layout_margin="24dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/Widget.CardContent">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:id="@+id/my_text_view"
        android:text="fub"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Lorem ipsum dolor sit amet"/>

</LinearLayout>

And for my code

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_view, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

screenshot here

Community
  • 1
  • 1
Tony Jiang
  • 11
  • 1
  • Not sure about your particular layout question, but `layout_width` and `layout_height` typically refer to content + padding, but not margin. Margin is considered outside the view. – Steve Blackwell Jan 17 '17 at 00:48

1 Answers1

-1

layout_width and layout_height in card view is the size of this card view. I think you fit this card view in its parent view. Whatever if you use "match_parent" in child view, your child view will fill its parent view and it will not fill the screen. So, you should check parent view of this card view.

StuckPixel
  • 111
  • 8
  • 1
    This doesn't answer my question. What is the "size"? Padding and content? Or padding, content, and margin? – Tony Jiang Aug 14 '16 at 05:33
  • Size is width and height of your view. layout_width is it's width and it cannot exceed its parent view. So, if you use padding in your child view, it will affect for its parent view. – StuckPixel Aug 14 '16 at 05:47