【问题标题】:What is it that android:layout_width and layout_height refer to? Does it include the margin?android:layout_width 和 layout_height 指的是什么?包括边际吗?
【发布时间】: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;
}

screenshot here

【问题讨论】:

  • 不确定您的特定布局问题,但layout_widthlayout_height 通常指的是内容+填充,而不是边距。边距被认为是在视图之外。

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


【解决方案1】:

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

【讨论】:

  • 这不能回答我的问题。什么是“大小”?填充和内容?还是填充、内容和边距?
  • Size 是视图的宽度和高度。 layout_width 是它的宽度,它不能超过它的父视图。因此,如果您在子视图中使用填充,它将影响其父视图。
猜你喜欢
  • 2016-05-20
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
相关资源
最近更新 更多