首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ImageView的CardView权重

ImageView的CardView权重
EN

Stack Overflow用户
提问于 2019-08-28 15:19:56
回答 2查看 102关注 0票数 0

我正在尝试创建一个CardView布局,其中图像占布局的2/3,其他1/3应该是文本视图。但不知何故,我得到了奇怪的结果,其中从来没有使用权重似乎总是图像占用其空间,无论我设置什么权重

这是我当前的布局

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:clipToPadding="false"
    android:orientation="horizontal"
    android:padding="3dp">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="15dp"
        android:layout_weight="1"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="15dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/testing"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher_background"
                app:layout_constraintBottom_toTopOf="@+id/info_text"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_weight="2" />

            <TextView
                android:id="@+id/info_text"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Testing Text"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/testing"
                app:layout_constraintVertical_weight="2" />

        </android.support.constraint.ConstraintLayout>

    </android.support.v7.widget.CardView>


</android.support.constraint.ConstraintLayout>

所以我总是希望使用该权重,而不是任何固定大小。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-28 15:30:03

设置android:layout_height="0dp"并像app:layout_constraintDimensionRatio="3:3"一样设置比率。它会解决你的身高问题。

以下是解决方案:

代码语言:javascript
运行
复制
<ImageView
   android:id="@+id/testing"
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:adjustViewBounds="true"
   android:contentDescription="@null"
   android:scaleType="centerCrop"
   android:src="@drawable/ic_launcher_background"
   app:layout_constraintBottom_toTopOf="@+id/info_text"
   app:layout_constraintDimensionRatio="1:1"
   app:layout_constraintHorizontal_bias="0.5"
   app:layout_constraintLeft_toLeftOf="parent"
   app:layout_constraintRight_toRightOf="parent"
   app:layout_constraintTop_toTopOf="parent"
   app:layout_constraintVertical_weight="2" />
票数 1
EN

Stack Overflow用户

发布于 2019-08-28 15:38:09

试试这个,可能会对你有帮助

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<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:clipToPadding="false"
    android:orientation="horizontal"
    android:padding="3dp">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="15dp"
        android:layout_weight="1"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="15dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/testing"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher_background" />

            <TextView
                android:id="@+id/info_text"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:gravity="center"
                android:text="Testing Text" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57686921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档