Ninepatch 背景缩小GridView的显示区域
我有一个基本的 GridView,设置如下:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainMenuGridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
一切正常,直到我添加九块背景。一旦我添加这样的背景图像:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainMenuGridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="@drawable/mainbackground"
/>
它会破坏我的网格视图。 gridview 中的所有图标都显示在左上角的一列中,而不是显示 3-4 列(取决于屏幕分辨率和方向)。此外,一次只显示两个,我必须向下滚动才能看到其余的。我怀疑 gridview 受到 ninpatch 中心徽标的尺寸限制,但我不确定。
我的 ninepatch 中间有一个小区域,上面有一个徽标,我希望它没有任何倾斜地显示。背景的其余部分是一个小渐变,占据了屏幕的其余部分。关于造成这种情况的原因有什么想法吗?
I've got a basic GridView that I've set up as follows:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainMenuGridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Everything works fine until I add a ninepatch background. Once I add the background image like this:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainMenuGridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="@drawable/mainbackground"
/>
It breaks my gridview. Instead of displaying 3-4 columns (depending on screen resolution and orientation) all icons in the gridview display in a single column in the upper left corner. Furthermore, only two at a time are displayed and I have to scroll down to see the rest. I suspect that the gridview is being bound by the dimensions of the ninpatch's center logo, but I'm not sure.
My ninepatch has a small area in the middle with a logo that I want to be displayed without any skewing. The rest of the background is a small gradient that takes up the remainder of the screen. Any ideas on what's causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您已经定义了填充(九个补丁底部和右侧的线)?尝试去掉这些,这样你就只定义了内容区域(顶部和左侧的线条)
Sounds like you have padding defined (the lines at the bottom and right of the ninepatch)? Try getting rid of those, so you only have content area defined (the lines at the top and left)
我有类似的问题。不幸的是,我想在我的例子中使用填充。我发现 gridLayout 上方法调用的顺序似乎有所不同。只要我在 setBackgroundResource() 之后调用 setPadding() ,一切都很好。
I had a similar problem. Unfortunately I want to use padding in my case. What I found is that the order of method calls on gridLayout seem to make a difference. As long as I called setPadding() after setBackgroundResource(), all is fine.