如何拉伸按钮以适合 GridView
我有三个 GrindView 的活动。我用自定义的 SimpleCursorAdapters 填充这些视图。每个单元格中都有一个按钮,我需要将其拉伸到 GrindView 的该单元格中。而且我还需要 GrindView 之间没有可用空间。我已经尝试设置填充/边距但没有效果。以下是 XML:
MainView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView android:id="@+id/gridview1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="5"
android:gravity="center" />
<GridView android:id="@+id/gridview2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="5"
android:gravity="center" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview3" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="4"
android:gravity="center" />
细胞:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:padding="1dip"
android:layout_height="fill_parent" android:orientation="horizontal">
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:padding="1dip"
android:layout_height="fill_parent" android:id="@+id/textButton" />
截屏: 当前状态
感谢您的帮助!
I have activity with three GrindViews. I fill these views with custom SimpleCursorAdapters. In each cell is button which I need to be stretched into that cell of GrindView. And also I need that there will be no free space between that GrindViews. I already try to set padding/margin with no effect. Here are the XMLs:
MainView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView android:id="@+id/gridview1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="5"
android:gravity="center" />
<GridView android:id="@+id/gridview2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="5"
android:gravity="center" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview3" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="4"
android:gravity="center" />
Cell:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:padding="1dip"
android:layout_height="fill_parent" android:orientation="horizontal">
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:padding="1dip"
android:layout_height="fill_parent" android:id="@+id/textButton" />
Screenshot:
Current Status
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您必须为按钮设置自己的可绘制图像,而不是使用标准系统图像。除非我弄错了,按钮的标准 MDPI 图像有一个内置的透明边框,水平边有 3 个像素,顶部有 1 个像素,底部有 4 个像素。因此,即使此按钮图像完全展开到 100% 填充网格单元格,它仍然会包含此透明边框,因此看起来不会完全填充单元格。 (这是基于默认按钮图像为 btn_default_normal.9.png 的假设。)
不使用按钮,请查看 ImageButton,允许您设置自己的图像。或者,要使用常规 按钮,您可能需要设置按钮的具有可绘制状态列表的背景图像。请查看“按钮样式”部分以获取更多信息。
--
但是,我现在在文档中看到,“显式设置填充值将覆盖在后台找到的相应填充。”我看到您提到您尝试设置填充,但是您是否尝试将填充设置为零?
I think you'll have to set your own drawable image for your buttons, and not use the standard system image. Unless I'm mistaken, the standard MDPI image for buttons has a built in transparent border of 3 pixels on the horizontal sides, 1 pixel on the top side, and 4 pixels on the bottom side. So, even if this button image were fully expanded to 100% fill a grid cell, it would still include this transparent border, and thus not appear to fully fill the cell. (This is based on the assumption that the default button image is btn_default_normal.9.png.)
Instead of using Buttons, take a look at ImageButton, which allows you to set your own image. Or, to use the regular Button, you may need to set the button's background image with a state list drawable. Take a look at the "Button style" section for more information.
--
However, I do see now in the docs, "Explicitly setting a padding value will override the corresponding padding found in the background." I see you mentioned you tried setting the padding, but did you try setting the padding to zero?