使用数据绑定时layout_height在布局文件中不起作用



我正在从后端服务器获取用户列表,并显示在回收器视图中。我每行都有一个布局文件。这是我尝试过的:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data class="UserDataBinding">
<variable
name="user"
type="com.example.myapp.User" />
</data>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/poster_path_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@null"
android:scaleType="centerCrop" />
//Other views
</androidx.cardview.widget.CardView>
</layout>

我的目标是仅显示个人资料图片。问题是,即使我将layout_height设置为150dp,图片显示的也非常小。即使我增加高度也没有任何反应。如何将卡片视图/图像视图设置为固定高度?


编辑:

我找到了解决方案。我忘了夸大视野。这条线解决了这个问题:

ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);

根据我们的讨论,我正在添加XML代码,您可以在其中根据自己进行网格布局,并对grid_layout_item进行基本更改

<GridView
android:id="@+id/mainGridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3F3F3"
android:columnWidth="200dp"
android:numColumns="4"
android:paddingStart="-2dp"
android:paddingTop="2dp"
android:paddingEnd="-2dp"
android:paddingBottom="2dp"
android:stretchMode="columnWidth">
</GridView>

在 CardView 中查看我的以下代码,它90dp根据您的需要进行更改。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_margin="2dp">
<LinearLayout
android:id="@+id/gridviewdata"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/services"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/ic_launcher_background"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/names"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/gridname"
android:textSize="12sp"
android:textAlignment="center"
android:layout_marginTop="@dimen/margin4"/>

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

任何进一步的更新和帮助,您可以在评论部分评论您的问题。 快乐编码。

正如@DavidWasser所建议的,我添加了解决方案作为我自己问题的答案。问题是我忘了夸大视野。这条线解决了我的问题:

ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);

最新更新