【发布时间】:2019-02-03 22:44:58
【问题描述】:
我是android开发的新手,我已经搜索过这个问题,但没有得到任何解决方案。我想问一下如何使cardview和recyclerview在一个屏幕上分成三块?
这是我的 XML
<LinearLayout 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"
android:layout_margin="1dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:id="@+id/card"
android:layout_height="250dp"
app:cardCornerRadius="4dp"
app:cardElevation="1dp"
app:cardMaxElevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.siyamed.shapeimageview.RoundedImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
这是我在 Activity 中尝试的代码
View rootLayout = findViewById(R.id.root_layout);
double height =rootLayout.getLayoutParams().height/3.0;
CardView card= (CardView)findViewById(R.id.card);
CardView.LayoutParams layoutParams= (CardView.LayoutParams) card.getLayoutParams();
layoutParams.height=(int)height;
layoutParams.width=MATCH_PARENT;
谢谢。
【问题讨论】:
-
定义 cardview 高度,如 android:layout_height =wrap_content 并设置最小值 android:minWidth="250dp"。
-
尝试这样实现 CardView card= (CardView)findViewById(R.id.card); // 设置 CardView layoutParams LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT, (int)height; ); card.setLayoutParams(params);
-
用recyclerview把cardview一屏分成三块?你能给出你想做的快照吗
标签: android layoutparams cardview