【发布时间】:2019-06-01 19:05:08
【问题描述】:
我有一个显示 RecyclerView 的片段,其中填充了一个对象数组 - 每个对象有 1 个 CardView。我希望我的应用程序显示 5 个 CardView 并让第 6 个 CardView 在屏幕上窥视,无论大小如何(通过将每个 CardView 的高度设置为整个屏幕高度的 18%)。我目前有这段代码可以将每个 CardView 的高度设置为整个屏幕的 1/6。这种方法的问题是我只能使每个 CardView 的高度为整个屏幕的 1/x,其中 x 只能是一个整数(我希望能够使每个 CardView 的高度成为屏幕高度的自定义百分比只能使其成为屏幕尺寸的 1/3、1/4、1/5 等)。
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Log.i(TAG, "WM " + wm);
Point size = new Point();
Display display = wm.getDefaultDisplay();
display.getSize(size);
int heighty = size.y;
int height = heighty/6; // does not work with doubles because setLayoutParams must be (int)
Log.i(TAG, "height " + heighty + " " + display);
CardView cm = (CardView)view.findViewById(R.id.card);
Log.i(TAG, "cm " + cm);
ViewGroup.LayoutParams params = cm.getLayoutParams();
Log.i(TAG, "params params " + params);
params.height= height;
params.width=MATCH_PARENT;
cm.setLayoutParams(params);
这是显示的每个 CardView 的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp"
android:id="@+id/card"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:weightSum="3"
android:orientation="vertical">
<TextView
android:id="@+id/user_name_iv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:textColor="#000000"
android:textSize="12sp"
android:layout_weight="0.7"
/>
<TextView
android:id="@+id/user_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:textColor="#000000"
android:textSize="12sp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:layout_weight="0.7"
/>
<ImageView
android:id="@+id/user_iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:contentDescription="@string/image"
android:layout_weight="1.5"/>
</LinearLayout>
我试图更改此实现以在 ConstraintLayout 中使用 LinearLayout,其中 LinearLayout 的高度是整个屏幕的 18%,LinearLayout 实例的高度(ImageView 和 2 个 textView)是线性布局。我用于该实现的 xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/cardview_id"
android:orientation="vertical"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.18"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:weightSum="1">
<TextView
android:id="@+id/user_name_iv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:layout_marginBottom="0dp"
android:layout_marginTop="10dp"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:textColor="#000000"
android:textSize="24sp"
/>
<TextView
android:id="@+id/user_desc"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:textColor="#000000"
android:textSize="14sp"
android:layout_below="@id/user_name_iv"
android:paddingRight="5dp"
android:paddingLeft="5dp"
/>
<ImageView
android:scaleType="centerCrop"
android:id="@+id/user_iv"
android:layout_height="0dp"
android:layout_weight="0.6"
android:layout_width="match_parent"
android:paddingRight="5dp"
android:paddingLeft="5dp"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
这使得 RecyclerView 的每个实例都达到我想要的高度(屏幕高度的 18%),但是我的 RecyclerView 使每个单独的实例都指定到一个屏幕,而不是使实例显示在另一个的正下方(我知道这是因为 ConstraintLayout 的高度是 match_parent,但是 wrap_content 使内容消失)。我想知道是否有理想的方法来完成我正在尝试做的事情或能够解决上述问题。
我的 RecyclerView xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/nestedView"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.v4.widget.NestedScrollView>
任何帮助将不胜感激,谢谢。
【问题讨论】:
标签: android android-layout android-recyclerview android-cardview android-layout-weight