【问题标题】:How to set the cardview height programmatically?如何以编程方式设置卡片视图高度?
【发布时间】:2019-02-03 22:44:58
【问题描述】:

我是android开发的新手,我已经搜索过这个问题,但没有得到任何解决方案。我想问一下如何使cardviewrecyclerview在一个屏幕上分成三块?


这是我的 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


【解决方案1】:

尝试获取窗口的高度并将其除以 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);

【讨论】:

  • 我尝试将代码放入 oncreate 中,在 setContentView() 下方。我得到“原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.ViewGroup$LayoutParams android.support.v7.widget.CardView.getLayoutParams()'”
  • 而不是使用 CardView.LayoutParams 使用 LayoutParams.LayoutParams
【解决方案2】:

正如@V-rund 建议的那样 获取屏幕/窗口高度并将其除以 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);

我希望这有助于解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 2019-07-10
    • 2013-09-03
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多