android - 无法动态更改 GridView
问题描述
我正在开发一个应用程序。在这个应用程序中,当您单击一个点时会打开一个警报对话框。在这个警告对话框中有两个编辑文本,一个网格视图和一个按钮。gridview 关联了一个使用 Glide 将 Uris 转换为 IconView(扩展 ImageView 的类)的 BaseAdapter。
我想设置 350dp 的最大高度,但如果只有一行(总是有一个图像),我希望 gridview 适应。
我尝试过这种方式:如何让 GridView 在添加项目时适应其高度
但它不起作用。所以我尝试计算元素的数量,如果超过 2 个,则设置 350dp 的高度,如果少于 2 个,则设置 175dp。但它不起作用。
警报对话框布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/puntoName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/puntoDescripcion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:maxHeight="100dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/puntoName" />
<GridView
android:id="@+id/photo_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:gravity="center"
android:horizontalSpacing="6dp"
android:numColumns="2"
android:verticalSpacing="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/puntoDescripcion"></GridView>
<Button
android:id="@+id/cerrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cerrar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/photo_grid" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我用来更改 GridView 高度的代码:
fun checkSize() {
val popUp: View = fragmentCaller.layoutInflater.inflate(R.layout.popup,null)
var photogrid: GridView = popUp.findViewById(R.id.photo_grid)
var params: ViewGroup.LayoutParams = photogrid.layoutParams
var tam: Int = 175
if (adapter.getDataSource().size>2) {
tam = DpToPixels(350)
}
photogrid.layoutParams.height = tam
photogrid.requestLayout()
}
private fun DpToPixels(dp: Int) : Int {
val escala: Float = fragmentCaller.requireContext().resources.displayMetrics.density;
var tam: Int = (dp * escala + 0.5f).toInt()
return tam
}
gridView 的大小始终为 0。不知道为什么。我只想以编程方式设置 android:layout_height 。我在调用 Dialog.show() 之后调用 checkSize() 方法,并且每次我对适配器的元素进行更改时。
谢谢。
解决方案
推荐阅读
- mysql - 您如何将 3 个表与 MySQL 中的 1 个表进行比较?
- c# - WCF - 从另一个 Web 应用程序(WCF 或 REST API)调用时出现 EndpointNotFoundException
- c - 如何解决c中的分段错误?
- firebase - Flutter 上的 Firebase 错误:无法解析配置“:firebase_core:debugCompileClasspath”的所有任务依赖项
- angular - 在移动断点之后在angular-gridster2中从左到右排序gridster项目
- c# - 收到错误消息:CS0029:无法隐式转换(ASP.NET Core MVC 和 C#)
- visual-studio-code - VSCode:在特定项目文件夹中创建文件的键盘快捷键?
- python - 更改拆分列名称并从表中捕获数据
- c# - 使用组策略将自定义 Windows 服务安装到所有域设备
- huawei-mobile-services - canvas组件绘制的图片无法在相册中显示