【发布时间】:2016-08-14 04:32:28
【问题描述】:
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;
}
【问题讨论】:
-
不确定您的特定布局问题,但
layout_width和layout_height通常指的是内容+填充,而不是边距。边距被认为是在视图之外。
标签: android android-layout android-recyclerview android-cardview