如何让图像按钮的尺寸在 Android 中有意义?
首先,我可以说,我发现布局 Android UI 是一种令人沮丧的体验吗?我曾经认为 XML 布局简单、干净、很棒,但每次我尝试用它做任何事情时,我都会花几个小时尝试做最简单的事情!
在这个特定的实例中,我试图制作一个简单的水平条,其中包含一个固定大小的图像按钮,在它的右侧和左侧我想要一个 ImageView 来占据剩余的可用宽度。我一直在用户界面中看到类似的结构:搜索时出现在屏幕顶部的搜索框、用于撰写文本/googletalk消息的文本区域和发送按钮等。
我尝试过水平线性布局和相对布局,我无法让按钮在任一布局中看起来正确。我最近的尝试有以下布局代码:
它看起来像这样:
使用 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是如何设置
RelativeLayout
的?尝试这样设置:希望这会有所帮助。
How did you set up your
RelativeLayout
? Try to set it up like this:Hope this helps.
如果尺寸正是您想要的尺寸,则在 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.