我想在RecyclerView中调整CardView的宽度,但是当我使用CardView.LayoutParams更改宽度时会抛出异常,有人帮助我吗?下面是代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvHour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="48dp"
android:alpha="0.7"
android:text="13:00"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_alignBottom="@+id/tvHour"
android:layout_alignStart="@+id/tvHour"
android:layout_marginBottom="6dp"
android:layout_marginStart="46dp"
android:alpha="0.12"
android:background="@color/black"
android:orientation="horizontal" />
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv_app"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignStart="@+id/tvHour"
android:layout_below="@+id/line"
android:layout_marginBottom="4dp"
android:layout_marginRight="6dp"
android:layout_marginStart="46dp"
android:visibility="gone"
card_view:cardBackgroundColor="@color/green"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="1dp">
<TextView
android:id="@+id/tv_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
android:padding="5dp"
android:textColor="@color/white"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
</RelativeLayout>
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
String hours = (String) mItemsData.get(position);
viewHolder.tvHour.setText(hours);
for (AppointmentModel model : mListAppointments) {
int hour = DateUtils.extractHour(DateUtils.stringToDate(model.getDate()));
if (formatHour(hour).equals(hours)) {
viewHolder.cvAppointment.setVisibility(View.VISIBLE);
Log.i("ARTHUR", String.valueOf(viewHolder.line.getWidth()));
viewHolder.tvDesc.setText(model.getMinutes());
break;
} else {
viewHolder.cvAppointment.setVisibility(View.GONE);
viewHolder.tvDesc.setText("");
}
}
}
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
答案 0 :(得分:3)
嗯,错误是不言自明的:
<div class="vertical">
<div class="progress-bar">
<div class="progress-track">
<div class="progress-fill" style="height: 100%; top: 0%; background: rgb(191, 231, 178);"></div>
<span style="color: rgb(0, 0, 0);">100%</span>
</div>
</div>
<div class="progress-bar">
<div class="progress-track">
<div class="progress-fill" style="height: 80%; top: 20%; background: rgb(248, 231, 153);"></div>
<span style="color: rgb(0, 0, 0);">80%</span>
</div>
</div>
<div class="progress-bar">
<div class="progress-track">
<div class="progress-fill" style="height: 60%; top: 40%; background: rgb(248, 231, 153);"></div>
<span style="color: rgb(0, 0, 0);">60%</span>
</div>
</div>
<div class="progress-bar">
<div class="progress-track">
<div class="progress-fill" style="height: 2%; top: 98%; background: rgb(248, 138, 138);">
<span style="color: rgb(0, 0, 0);">0%</span>
</div>
</div>
</div>
</div>
您正在执行操作的视图属于java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
,您尝试以RelativeLayout