gpt4 book ai didi

android - 将 CardView 的高度设置为屏幕尺寸的自定义百分比

转载 作者:行者123 更新时间:2023-12-05 06:26:14 48 4
gpt4 key购买 nike

我有一个显示 RecyclerView 的 fragment ,其中填充了对象数组 - 每个对象 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's)的高度是线性布局。我用于该实现的 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>

非常感谢任何帮助,谢谢。

最佳答案

我找到了解决方案。对于遇到此问题并希望得到答案的任何人,我修改了上面的 Java 代码,使每个 cardView 的高度为屏幕高度的任意分数。

        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;
double heightDouble = heighty/2.5; // this will make the screen display 2 cardview's and have half of the third one peeking. replace 2.5 with any decimal you desire to make your last cardview peek a certain amount
int height = (int) Math.rint(heightDouble);



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);

关于android - 将 CardView 的高度设置为屏幕尺寸的自定义百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56409633/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com