【发布时间】:2020-11-07 23:31:46
【问题描述】:
有一个网格视图,其中每个网格项包含 2 个图像和 1 个文本视图。
主图像“subjectIcon”始终显示,并且在下面的示例中是带有听诊器的红色方块。第二张图片“favouriteStatus”是一个小星星图片,应该显示在右上角。文本位于“subjectIcon”图像下方。
代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="120dp"
android:layout_height="170dp">
<ImageView
android:id="@+id/subjectIcon"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:padding="0dp"
android:scaleType="fitCenter"
android:visibility="visible"
app:srcCompat="@drawable/imagescenario" />
<ImageView
android:id="@+id/favouriteStatus"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="84dp"
android:layout_marginTop="0dp"
android:scaleType="fitCenter"
android:visibility="visible"
app:srcCompat="@drawable/star_fill_drop" />
<TextView
android:layout_width="120dp"
android:layout_height="50dp"
android:id="@+id/subjectTitle"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_marginTop="120dp"
android:text="Subject Title"/>
设计视图显示了这一点,这正是我想要的:
但是,当我运行应用程序时,从未显示星形图像。有趣的是,如果我将文本放在主图像的顶部,它会位于顶部并显示。
我的头撞墙了 3 个小时,我错过了什么? 提前致谢
网格视图 xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".SubjectListActivity">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:columnWidth="120dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="5dp"
android:stretchMode="spacingWidthUniform"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
-
嗨,我看不出您如何关联
RelativeLayout中的三个小部件 .. 我的意思是您没有添加相对布局属性来定义每个小部件的位置 .. 还有你在哪里使用gridView? -
抱歉 Zain 我不知道你在问什么? xml 是一个网格项目。设计视图准确显示每个网格项目应如何显示,但在应用程序中,第二张图像(星形)从不显示