gpt4 book ai didi

java - Android Studio - 图像展开按钮(并打破重量规则?)

转载 作者:行者123 更新时间:2023-12-02 12:57:12 26 4
gpt4 key购买 nike

我已经解决一个问题太久了。每当我将图像放入 ImageButton 时,该按钮都会扩展为非常大。为了尝试解决这个问题,我创建了一个带有 TableRows 的 TableLayout,其中 TableLayout 有一个weightSum,每行都有自己的layout_weight。然而,无论我为一行分配多少权重,图像都会爆炸并压缩所有其他行。换句话说,TableRows 不尊重分配给它们的layout_weight。

下面有四行,权重分别为 4、5、3、1。 ImageButton 位于第三行,layout_weight="3"。但是,正如您所看到的,该行会扩展以适合按钮图像。

Preview

无论如何,当我在屏幕上有多个按钮时,这是一个主要问题。我创建了许多尺寸相同的按钮,认为我会将它们做得很大,这样它们就可以很好地适应更大的设备。问题再次在于它们不会缩小以适合按钮内部,而是扩展按钮,从而破坏了该过程中的所有布局权重。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="13"
android:background="@drawable/edit_users_view">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"/>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5">
<ScrollView>
<!-- Figure out button first -->
</ScrollView>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_cancel_button"/>

</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</TableRow>
</TableLayout>

此外 - 这个问题可能部分与我存储图像的方式有关。我已将所有内容保存到我的可绘制文件夹中,但我的 mipmap 文件夹中没有任何内容。不过,我确实尝试将这个取消按钮保存到 mipmap-mdpi 文件夹中并将其加载到 ImageButton 中,但效果完全相同。 Mipmap 文件夹可能只是设置分辨率,而不是大小?我没有展开我的可绘制文件夹,因为里面可能有 50 个图像/选择器。

enter image description here

最佳答案

<ImageButton
android:layout_width="XXdp"
android:layout_height="XXdp"
android:background="@drawable/selector_cancel_button"/>

为你的ImageButton设置一个特定的密度像素,它将限制它的扩展。

这个答案更像是HACK。正确的方法是为所有屏幕尺寸提供不同的图像。

ldpi、mdpi、hdpi、xhpdi、xxhdpi、xxxhdpi。 Stack 上有多个开放线程讨论如何实现这一目标。希望有帮助。

关于java - Android Studio - 图像展开按钮(并打破重量规则?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44396460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com