CardView layout_width =“match_parent”가 상위 RecyclerView 너비와 일치하지 않습니다. android:layout_height=”match_parent” android:layout_margin=”20dp”

나는 layout_width = “match_parent”와 함께 RecyclerView를 포함하는 조각이 있습니다.

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment" />

RecyclerView의 항목은 layout_width = “match_parent”도있는 CardView입니다.

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    card_view:cardCornerRadius="4dp">

    <TextView
        android:layout_gravity="center"
        android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent"
        android:textAppearance="?android:textAppearanceLarge"/>
</android.support.v7.widget.CardView>

다음과 같이 항목보기를 부풀립니다.

public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        CardView v = (CardView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_listitem, null, true);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

하지만 앱을 실행할 때 CardView는 아래와 같이 wrap_content로 렌더링됩니다.

카드 버그

이것은 실제 장치가 아닌 에뮬레이터에서 실행되었습니다.

내가 뭘 잘못하고 있습니까? 아니면 버그입니까?



답변

에 대한 문서 inflate:

지정된 xml 리소스에서 새보기 계층을 확장합니다. 오류가있는 경우 InflateException이 발생합니다.

로드 할 XML 레이아웃 리소스의 매개 변수
리소스 ID (예 : R.layout.main_page) 루트
가 생성 된 계층의 부모 (attachToRoot가 true 인 경우) 또는 단순히 루트에 대한 LayoutParams 값 집합을 제공하는 객체가됩니다. 반환 된 계층 구조 (attachToRoot가 false 인 경우)
attachToRoot 부풀려진 계층 구조가 루트 매개 변수에 첨부되어야하는지 여부? false 인 경우 루트는 XML의 루트 뷰에 대한 LayoutParams의 올바른 하위 클래스를 만드는 데만 사용됩니다. 부풀려진 계층 구조의 루트 뷰를 반환합니다. root가 제공되고 attachToRoot가 true이면 이것은 root입니다. 그렇지 않으면 확장 된 XML 파일의 루트입니다.

여기에 중요 하지 진정한 제공하지만 않는 부모를 공급 :

LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_listitem, parent, false);

공급 parent보기 것은 사용의 LayoutParams 무엇 인플레이터의 노하우를 할 수 있습니다. false매개 변수를 제공하면 아직 부모에 연결 하지 않도록 지시합니다 . 그것이 RecyclerView당신을 위해 할 일입니다.


답변

RelativeLayout을 CardView의 직계 부모로 사용합니다.

<RelativeLayout 
    android:layout_width="match_parent" ... >
    <CardView 
        android:layout_width="match_parent" ... >
    </CardView>
</RelativeLayout>

답변

이것은 나를 위해 일했습니다.

 View viewHolder= LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item, null, false);
 viewHolder.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
 return new ViewOffersHolder(viewHolder);

답변

내가 한 방법은 다음과 같습니다.

View view = mInflater.inflate(R.layout.row_cardview, null, true);
            WindowManager windowManager = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
            int width = windowManager.getDefaultDisplay().getWidth();
            view.setLayoutParams(new RecyclerView.LayoutParams(width, RecyclerView.LayoutParams.MATCH_PARENT));

설명 :
onCreateViewHolde에서 카드보기가 표시되면 화면 너비를 가져온 다음 카드보기에 맞게 레이아웃 매개 변수를 설정하십시오.


답변

이것은 수정 된 것으로 보입니다 'com.android.support:recyclerview-v7:23.2.1'

자세한 내용은 RecyclerView wrap_content 를 참조하세요 .


답변

새 레이아웃 매개 변수를 설정하기 만하면됩니다.

view.setLayoutParams(new RecyclerView.LayoutParams(
    RecyclerView.LayoutParams.MATCH_PARENT,
    RecyclerView.LayoutParams.WRAP_CONTENT
));

답변

기본 구현 은 기본 레이아웃 관리자에서 wrap_content 사용을 강제합니다 .

  @Override
public LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
}

다음과 같이 LayoutManager에서이 메서드를 재정의하기 만하면됩니다.

  @Override
public LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
}