android:layout_width和layout_height是指什么?它是否包括保证金?

时间:2016-08-14 04:32:28

标签: android android-layout android-recyclerview android-cardview

似乎Android layout_width和layout_height仅包含视图的内容和填充。如果是这样,为什么以下代码有效?

我已阅读this post,但是,虽然我理解在这种情况下需要做什么,但我不明白为什么会有效。

我正在使用RecyclerView,其代码与链接示例类似。 RecyclerView的每个项目都是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>

我的代码

@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

1 个答案:

答案 0 :(得分:-1)

卡片视图中的layout_width和layout_height是此卡片视图的大小。我认为您在其父视图中适合此卡片视图。无论您在子视图中使用“match_parent”,您的子视图都将填充其父视图,并且它不会填满屏幕。因此,您应该检查此卡片视图的父视图。