0

看來,在Android layout_width和layout_height僅包括所述內容和所述視圖的填充。如果是這樣,爲什麼下面的代碼工作?它是什麼,安卓: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

+0

不確定您的特定佈局的問題,但'layout_width'和'layout_height'通常是指內容+填充,而不是利潤率。保證金被視爲不在視野範圍之內。 –

回答

-1

layout_width和layout_height卡的看法是這樣的卡片視圖的大小。我認爲你在其父視圖中適合這張卡片視圖。無論你在子視圖中使用「match_parent」,你的子視圖將填充它的父視圖,並且它不會填滿屏幕。所以,你應該檢查這張卡片視圖的父視圖。

+1

這不回答我的問題。什麼是「大小」?填充和內容?或填充,內容和邊距? –

+0

大小是您的視圖的寬度和高度。 layout_width是它的寬度,它不能超過它的父視圖。所以,如果你在你的子視圖中使用填充,它會影響到它的父視圖。 – Zayar

相關問題