Android 基于文本的RecyclerView中CardView的动态行高
我希望制作一个基于文本内容动态调整自身大小的CardView(即,当文本对于某些屏幕来说太长并且需要多行时) 所需(这是通过将Android 基于文本的RecyclerView中CardView的动态行高,android,android-layout,android-recyclerview,android-cardview,Android,Android Layout,Android Recyclerview,Android Cardview,我希望制作一个基于文本内容动态调整自身大小的CardView(即,当文本对于某些屏幕来说太长并且需要多行时) 所需(这是通过将行属性修改为2实现的): 但是,在测试和预览中,都会发生以下情况: " 我不确定解决这个问题的可能方法是什么。必须以编程方式设置行属性,还是我的XML布局有问题,或者RecyclerView的某些设置有问题?我的RecyclerView使用简单的LinearLayoutManager作为其布局管理器 CardView的XML如下所示: <?xml version=
行
属性修改为2实现的):
但是,在测试和预览中,都会发生以下情况:
"
我不确定解决这个问题的可能方法是什么。必须以编程方式设置行
属性,还是我的XML布局有问题,或者RecyclerView的某些设置有问题?我的RecyclerView使用简单的LinearLayoutManager
作为其布局管理器
CardView的XML如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/cv_uav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="2dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:tint="#4b4b4b"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_menu_quadcopter" />
<TextView
android:id="@+id/uav_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:singleLine="false"
android:text="Telemedicine Drone Approaching!"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="parent" />
<TextClock
android:id="@+id/uav_eta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginStart="8dp"
android:format24Hour="mm:ss"
android:text="00:23"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView4" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
android:text="ETA:"
android:textAppearance="@android:style/TextAppearance.Material.Large"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView3" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
用您现有的代码替换上述代码,我很难用鼠标管理
ConstraintLayout
,因此我将其更改为LinearLayout
,这将实现您想要实现的目标。在您的上下文中创建列表行CardView包装内容。尽管您可以在onBindViewHolder内的recycler视图中动态添加内容的高度,如下所示
float proportionalHeight = getScreenWidth(yourViewHeight.getHeight(), yourViewWidth.getWidth());
if (proportionalHeight > 380) {
proportionalHeight = 380;
}
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, proportionalHeight, resources.getDisplayMetrics());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) px);
yourMainView.setLayoutParams(params);
private float getScreenWidth(int height, int width) {
Configuration configuration = getActivity().getResources().getConfiguration();
float screenWidthDp = configuration.screenWidthDp;
float check = (float) height / width;
Log.e("TAG", "getScreenWidth: " + screenWidthDp);
Log.e("TAG", "return: " + check * screenWidthDp);
return check * screenWidthDp;
}
如果您不想坚持使用
ConstraintLayout
aLinearLayout
可以简单地使其工作。
float proportionalHeight = getScreenWidth(yourViewHeight.getHeight(), yourViewWidth.getWidth());
if (proportionalHeight > 380) {
proportionalHeight = 380;
}
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, proportionalHeight, resources.getDisplayMetrics());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) px);
yourMainView.setLayoutParams(params);
private float getScreenWidth(int height, int width) {
Configuration configuration = getActivity().getResources().getConfiguration();
float screenWidthDp = configuration.screenWidthDp;
float check = (float) height / width;
Log.e("TAG", "getScreenWidth: " + screenWidthDp);
Log.e("TAG", "return: " + check * screenWidthDp);
return check * screenWidthDp;
}