Android ImageViews 加权时无法正常工作

发布于 2024-11-26 23:04:02 字数 1986 浏览 3 评论 0原文

这段 XML 将正确显示一个覆盖整行的按钮,然后显示 2 个均匀加权并共享 50% 空间的按钮,如下所示:

__________________________
|________________________|
||       ButtonA        ||
||______________________||
|____________ ___________|
|| ButtonB  | | ButtonC ||
||__________| |_________||
|________________________|

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_height="fill_parent" android:layout_width="fill_parent">
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent"
 android:layout_weight="1">
    <Button android:id="@+id/b_a"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button A" />
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent"
 android:layout_weight="1">
    <Button android:id="@+id/b_b"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button B" />
    <Button android:id="@+id/b_c"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button C" />
</TableRow>

我想用 ImageView 替换按钮 A,它将像按钮 A 当前一样拉伸。但是,当您将 ButtonA 替换为:

<ImageView android:id="@+id/img_a"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src=@drawable/imagename />

这会影响按钮 B 和 C 的权重,使按钮 B 覆盖大部分屏幕,并使按钮 C 真正被压扁。它似乎与图像的尺寸有关(我使用的是 320x50)。

__________________________
|________________________|
||       Image A        ||
||______________________||
|___________________ ____|
|| ButtonB          ||bC||
||__________________||__||
|________________________|

如何插入此 ImageView 而不影响表的其余行?

This piece of XML will correctly display a button which covers the entire row then 2 buttons which are weighted evenly and share 50% of the space like so:

__________________________
|________________________|
||       ButtonA        ||
||______________________||
|____________ ___________|
|| ButtonB  | | ButtonC ||
||__________| |_________||
|________________________|

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_height="fill_parent" android:layout_width="fill_parent">
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent"
 android:layout_weight="1">
    <Button android:id="@+id/b_a"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button A" />
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent"
 android:layout_weight="1">
    <Button android:id="@+id/b_b"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button B" />
    <Button android:id="@+id/b_c"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button C" />
</TableRow>

I want to replace Button A with an ImageView which will stretch across just like Button A is currently. However, when you replace ButtonA with:

<ImageView android:id="@+id/img_a"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src=@drawable/imagename />

This affects the weighting of Buttons B and C, making button B cover most of the screen and making C really squashed up. It appears to be related to the dimensions of the image (I used 320x50).

__________________________
|________________________|
||       Image A        ||
||______________________||
|___________________ ____|
|| ButtonB          ||bC||
||__________________||__||
|________________________|

How can I insert this ImageView without affecting the rest of the table rows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短暂陪伴 2024-12-03 23:04:02

我认为你对layout_weight的使用有点过于自由了。这更多的是用于在布局中包含多个项目的选项,这些项目需要根据屏幕尺寸动态调整大小(就像按钮 B 和 C 一样)。我会删除其余的布局权重,因为它们本质上没有做任何事情,并且可能有一些潜在的时髦行为。另外,layout_weight 是项目大小与屏幕的比率,范围为 0-1。按钮 B 和 C 都希望拥有 100% 的水平空间分配给它们。因此,我相信您的问题是按钮 B 的优先级高于按钮 C。尝试将这 2 个项目的布局权重更改为 0.5,这样它们每个都需要 50% 的分配空间。让我知道这对您有何作用!

I think you are being a little too liberal with your use of layout_weight. this is more an option used for having multiple items within a layout that need to be sized dynamically according to screen size (just like you have with buttons B and C). I would get rid of the rest of your layout_weights as they essentially arent doing anything and may have some underlying funky behavior. Also, layout_weight is a ratio of item-size to screen that ranges from 0-1. Both buttons B and C want to have 100% of the horizontal space allocated for them. Thus, I believe your issue is that button B is being prioritized higher than button C. try changing your layout_weight's for those 2 items to 0.5, This way they each desire 50% of the space allocated. Let me know how this works for you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文