专注于 GridView 布局

发布于 2024-11-01 13:17:24 字数 1678 浏览 1 评论 0原文

在我的 Android 应用程序中,我有一个带有一组按钮的 GridView 布局。问题是,我无法正确设置按钮上的焦点(我的意思是光标,由 Android 设备上的操纵杆控制)。我可以用图片描述我的问题:

Focus in GridView 布局

我在 BaseAdapter 的代码中动态地将按钮设置为 GridView使用 LayoutInflater。在代码中,我设置了按钮的文本、图像(CompoundDrawable)和背景颜色,因为我的按钮就像一个复选框(蓝色背景意味着选中)。

看起来程序正在关注网格字段而不是按钮本身。我的意思是像表格的单元格而不是表格中的按钮。因为焦点颜色是默认的(绿色),但我在选择器中设置了蓝色。对焦也不起作用。焦点显然位于按钮后面,超出了按钮的范围。有人可以帮我解决这个麻烦吗?我的 XML 布局的代码:

main.xml:

...
<GridView
    android:id="@+id/channels"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="6dip"
    android:verticalSpacing="10dip"
    android:horizontalSpacing="10dip"
    android:numColumns="auto_fit"
    android:columnWidth="60dip"
    android:stretchMode="columnWidth"
    android:gravity="center"        
    android:background="@color/container_main">
</GridView>
...

channel.xml:

<Button
    android:id="@+id/channel"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:gravity="center_horizontal"         
    android:background="@color/willBeSetInAdapter" <!-- white/blue/darkblue focus background -->
    android:drawableTop="@drawable/willBeSetInAdapter" <!-- icon -->
    android:drawablePadding="0dip"         
    android:text="WillBeSetInAdapter" <!-- label text -->
    android:textColor="@color/text_container_main"
    android:textSize="12sp" 
    android:focusable="true" 
    android:focusableInTouchMode="false">
</Button>

我尝试在 Button 和 GridView 中设置焦点参数,并尝试了很多方法,但不幸的是它仍然不起作用:(

In my Android app, I have a GridView layout with a set of Buttons. The problem is, that I can't set properly the focus (I mean cursor, controlled with joystick on the Android device) on the buttons. I can describe my problem with a picture:

Focus in GridView layout

I set the Buttons to the GridView dynamically in the code in BaseAdapter using LayoutInflater. In the code, I set button's text, image (CompoundDrawable) and background color, because my buttons works like a checkboxes (blue background means checked).

It seems like the program is focusing the grid field and not the button itself. I mean something like cell of table and not the button in table. Because the focus color is default (green) however I set the blue color in selector. Focus press also doesn't work. And the focus is evidently behind the button, out of button's bounds. Can somebody help me with this trouble please? Code of my XML layouts:

main.xml:

...
<GridView
    android:id="@+id/channels"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="6dip"
    android:verticalSpacing="10dip"
    android:horizontalSpacing="10dip"
    android:numColumns="auto_fit"
    android:columnWidth="60dip"
    android:stretchMode="columnWidth"
    android:gravity="center"        
    android:background="@color/container_main">
</GridView>
...

channel.xml:

<Button
    android:id="@+id/channel"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:gravity="center_horizontal"         
    android:background="@color/willBeSetInAdapter" <!-- white/blue/darkblue focus background -->
    android:drawableTop="@drawable/willBeSetInAdapter" <!-- icon -->
    android:drawablePadding="0dip"         
    android:text="WillBeSetInAdapter" <!-- label text -->
    android:textColor="@color/text_container_main"
    android:textSize="12sp" 
    android:focusable="true" 
    android:focusableInTouchMode="false">
</Button>

I tried to set focus parametres in Button and also in GridView and tried a lot of things, but unfortunately it still doesn't work :(

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

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

发布评论

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

评论(2

御守 2024-11-08 13:17:24

为 GridView 设置 android:descendantFocusability="afterDescendants" 解决了我的问题。因为程序聚焦于网格字段而不是按钮本身。使用descendantFocusability 参数,我禁止聚焦网格字段,只允许聚焦按钮。

Setting android:descendantFocusability="afterDescendants" for GridView solved my problem. Because program focused the grid field and not the button itself. Using descendantFocusability parameter I forbid to focus grid field and allow only to focus the buttons.

糖果控 2024-11-08 13:17:24

您是否尝试过为您的 GridView 设置 android:drawSelectorOnTop="true" ?

Have you tried setting android:drawSelectorOnTop="true" for your GridView?

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