为什么我的带有 CardView 的 RecyclerView 无法设置宽度以匹配父级?
Why my RecyclerView with CardView can't set width to match parent?
我想用列表在 CardView 中一一显示我的 RecyclerView,但我认为我的 RecyclerView 或 CardView 无法设置宽度以匹配父项。
这是我的 Activity 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:gravity="center_horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/rexyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="5dp"
card_view:cardCornerGroup="5dp"
android:scrollbars="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
这是我的 CardView 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cardview"
android:elevation="3dp"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="1dp"
card_view:cardElevation="4dp"
>
<!--
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<TextView
android:layout_width="130sp"
android:layout_height="wrap_content"
android:id="@+id/tmpkode"
android:textSize="11sp"
android:maxLines="1"
/>
<TextView
android:layout_width="70sp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tmpkode"
android:layout_marginLeft="10dp"
android:maxLines="1"
android:textSize="11sp"
android:id="@+id/tmptgla"
android:textAlignment="center"
/>
<TextView
android:layout_width="50sp"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_toRightOf="@id/tmptgla"
android:id="@+id/tmpnamaz"
android:maxLines="1"
android:layout_marginLeft="10dp"
android:textAlignment="center"
/>
<TextView
android:layout_width="100sp"
android:textAlignment="center"
android:layout_toRightOf="@id/tmpnamaz"
android:layout_height="wrap_content"
android:maxLines="1"
android:textSize="11sp"
android:layout_marginLeft="10dp"
android:id="@+id/namasup"/>
<TextView
android:layout_width="130sp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/namasup"
android:textSize="11dp"
android:id="@+id/nopenawar"
android:textAlignment="center"/>
</RelativeLayout>
<!-- </LinearLayout> -->
</android.support.v7.widget.CardView>
</LinearLayout>
我的适配器:
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View Itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.card, parent, false);
return new ViewHolder(Itemview);
}
我的 Activity :
setContentView(R.layout.approvalpersetujuan);
recyclerView = (RecyclerView)findViewById(R.id.rexyclerview);
data = new ArrayList<>();
Prosesdataserver(0);
gridLayoutManager = new GridLayoutManager(this, 5);
recyclerView.setLayoutManager(gridLayoutManager);
如果你需要网格布局管理器,你可以试试这个
gridLayoutManager = new GridLayoutManager(this, 1);
或者您可以像这样使用水平线性布局管理器
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
并将其设置为您的回收站视图
我想用列表在 CardView 中一一显示我的 RecyclerView,但我认为我的 RecyclerView 或 CardView 无法设置宽度以匹配父项。
这是我的 Activity 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:gravity="center_horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/rexyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="5dp"
card_view:cardCornerGroup="5dp"
android:scrollbars="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
这是我的 CardView 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cardview"
android:elevation="3dp"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="1dp"
card_view:cardElevation="4dp"
>
<!--
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<TextView
android:layout_width="130sp"
android:layout_height="wrap_content"
android:id="@+id/tmpkode"
android:textSize="11sp"
android:maxLines="1"
/>
<TextView
android:layout_width="70sp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tmpkode"
android:layout_marginLeft="10dp"
android:maxLines="1"
android:textSize="11sp"
android:id="@+id/tmptgla"
android:textAlignment="center"
/>
<TextView
android:layout_width="50sp"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_toRightOf="@id/tmptgla"
android:id="@+id/tmpnamaz"
android:maxLines="1"
android:layout_marginLeft="10dp"
android:textAlignment="center"
/>
<TextView
android:layout_width="100sp"
android:textAlignment="center"
android:layout_toRightOf="@id/tmpnamaz"
android:layout_height="wrap_content"
android:maxLines="1"
android:textSize="11sp"
android:layout_marginLeft="10dp"
android:id="@+id/namasup"/>
<TextView
android:layout_width="130sp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/namasup"
android:textSize="11dp"
android:id="@+id/nopenawar"
android:textAlignment="center"/>
</RelativeLayout>
<!-- </LinearLayout> -->
</android.support.v7.widget.CardView>
</LinearLayout>
我的适配器:
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View Itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.card, parent, false);
return new ViewHolder(Itemview);
}
我的 Activity :
setContentView(R.layout.approvalpersetujuan);
recyclerView = (RecyclerView)findViewById(R.id.rexyclerview);
data = new ArrayList<>();
Prosesdataserver(0);
gridLayoutManager = new GridLayoutManager(this, 5);
recyclerView.setLayoutManager(gridLayoutManager);
如果你需要网格布局管理器,你可以试试这个
gridLayoutManager = new GridLayoutManager(this, 1);
或者您可以像这样使用水平线性布局管理器
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
并将其设置为您的回收站视图