如何以编程方式设置卡片视图高度?
How to set the cardview height programmatically?
我是 android 开发的新手,我已经搜索了这个问题,但没有得到任何 solution.I 想问一下如何使 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;
谢谢。
尝试获取 window 的高度并将其除以 3,
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card= (CardView)findViewById(R.id.card);
LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) card.getLayoutParams();
layoutParams.height= height;
layoutParams.width=MATCH_PARENT;
card.setLayoutParams(layoutParams);
正如@V-rund建议的那样
获取 Screen/Window 高度并使用
除以 3
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card = (CardView)findViewById(R.id.card);
但不要使用 CardView.LayoutParams
,而是使用 LayoutParams
作为 CardView 的父级布局,在您的情况下将是 LinearLayout
。
LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams)
card.getLayoutParams();
layoutParams.height = height;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
card.setLayoutParams(layoutParams);
希望对您的问题有所帮助。
我是 android 开发的新手,我已经搜索了这个问题,但没有得到任何 solution.I 想问一下如何使 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;
谢谢。
尝试获取 window 的高度并将其除以 3,
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card= (CardView)findViewById(R.id.card);
LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) card.getLayoutParams();
layoutParams.height= height;
layoutParams.width=MATCH_PARENT;
card.setLayoutParams(layoutParams);
正如@V-rund建议的那样 获取 Screen/Window 高度并使用
除以 3DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card = (CardView)findViewById(R.id.card);
但不要使用 CardView.LayoutParams
,而是使用 LayoutParams
作为 CardView 的父级布局,在您的情况下将是 LinearLayout
。
LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams)
card.getLayoutParams();
layoutParams.height = height;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
card.setLayoutParams(layoutParams);
希望对您的问题有所帮助。