我正在尝试将CardView宽度与屏幕宽度相匹配,但CardView宽度与屏幕宽度不匹配。我一次又一次地检查了我的代码,我甚至将所有属性宽度都设置为匹配parent。
Recyclerview xml -
import threading
port_controller = threading.Thread(target=check_presence, args=(arduino_port, 0.1,))
port_controller.setDaemon(True)
port_controller.start()
卡片视图xml -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayTodayExercises">
<android.support.v7.widget.RecyclerView
android:id="@+id/exercises_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
充气代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_general_dimen"
android:orientation="horizontal">
<TextView
android:id="@+id/exer_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_height="40dp"
android:textStyle="bold"
android:gravity="center_vertical"
tools:text="Exercise Name"/>
<TextView
android:id="@+id/se_eps"
android:layout_width="0dp"
android:layout_weight="1"
android:textStyle="bold"
android:layout_marginLeft="@dimen/sets_reps_dimen"
android:gravity="center_vertical"
android:layout_height="40dp"
tools:text="5 x 6"/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_marginLeft="@dimen/sets_reps_dimen"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
</LinearLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:1)
问题在于你的inflater。查看LayoutInflater的文档,特别是inflate方法。
试试这个:
LayoutInflater.from(parent.getContext())
.inflate(R.layout.exercises_card_view_layout, parent, false);
不提供null,而是提供父视图。这样,inflater就知道要使用什么LayoutParameters。提供false参数告诉LayoutInflater不要将它附加到父级。 RecyclerView将为您处理此事。
答案 1 :(得分:0)
这是一个众所周知的问题。您可以使用例如RelativeLayout包装CardView或从此处尝试解决方案:CardView layout_width="match_parent" does not match parent RecyclerView width