如何让图像按钮的尺寸在 Android 中有意义?

发布于 2024-10-21 02:47:07 字数 693 浏览 7 评论 0原文

首先,我可以说,我发现布局 Android UI 是一种令人沮丧的体验吗?我曾经认为 XML 布局简单、干净、很棒,但每次我尝试用它做任何事情时,我都会花几个小时尝试做最简单的事情!

在这个特定的实例中,我试图制作一个简单的水平条,其中包含一个固定大小的图像按钮,在它的右侧和左侧我想要一个 ImageView 来占据剩余的可用宽度。我一直在用户界面中看到类似的结构:搜索时出现在屏幕顶部的搜索框、用于撰写文本/googletalk消息的文本区域和发送按钮等。

我尝试过水平线性布局和相对布局,我无法让按钮在任一布局中看起来正确。我最近的尝试有以下布局代码:

它看起来像这样:

Screnshot

使用 hiearchyviewer 表示 imageview 和按钮具有相同的高度 (45px)。它显示的视图尺寸和位置正是我正在寻找的。相同的高度(当然宽度不同,因为 ImageView 更宽)。它们紧邻在一起,位于相对布局的中心。然而,屏幕上绘制的按钮显然没有占据整个 ImageButton 视图。我认为用于 ImageButton 背景的 android 系统 9patch 可绘制对象有点奇怪。但我知道什么?无论我如何尝试,我都无法让它看起来正确。

First of all, can I just say, I find laying out android UI's to be a frustrating experience? I used to think the XML layouts were simple and clean and awesome but every time I try to make anything with it I spend hours trying to do the simplest things!

In this particular instance I'm trying to make a simple horizontal bar that contains an image button of fixed size on the right and to the left of it I want an ImageView that takes up the rest of the available width. I see similar constructs all the time in the UI: the search box that appears at the top of the screen when searching, the text area and send button for composing text/googletalk messages, etc.

I've tried both a horizontal linear layout and a relative layout, and I can't get the button to look right in either one. My latest attempt has the following layout code:

It looks like this:

Screnshot

Using the hiearchyviewer indicates that both the imageview and the button have the same height (45px). And it shows the view dimensions and positions to be exactly what I'm looking for. Same height (differing widths of course since the ImageView is much wider). And they butt right up next to each other, centered in the Relative Layout. However the button as drawn on screen is obviously not taking up the full ImageButton view. I'm thinking it's something weird about the android system 9patch drawable used for the ImageButton background. But what do I know? I can't get it to look right no matter what I try.

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

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

发布评论

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

评论(2

梦在夏天 2024-10-28 02:47:07

您是如何设置 RelativeLayout 的?尝试这样设置:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" android:layout_height="wrap_content">
    <ImageButton android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:id="@+id/imgButton"></ImageButton>
    <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:src="@drawable/red_button" android:scaleType="fitXY" android:layout_alignBottom="@+id/imgButton" android:layout_alignTop="@+id/imgButton" android:layout_toLeftOf="@+id/imgButton"></ImageView>        
</RelativeLayout>

希望这会有所帮助。

How did you set up your RelativeLayout? Try to set it up like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" android:layout_height="wrap_content">
    <ImageButton android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:id="@+id/imgButton"></ImageButton>
    <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:src="@drawable/red_button" android:scaleType="fitXY" android:layout_alignBottom="@+id/imgButton" android:layout_alignTop="@+id/imgButton" android:layout_toLeftOf="@+id/imgButton"></ImageView>        
</RelativeLayout>

Hope this helps.

丑丑阿 2024-10-28 02:47:07

如果尺寸正是您想要的尺寸,则在 ImageButton 和 ImageView 中,使用 android:scaleType="fitXY" 并检查。

对于简单的情况,我可能会使用水平方向的线性布局,其中有两个具有适当权重的按钮。

If dimensions are exactly how you are looking for , then in ImageButton and ImageView , use android:scaleType="fitXY" and check.

For simple case , I might use linearlayout with horizontal orientation with two buttons in it with proper weights.

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