android - 焦点在 GridView 布局中

标签 android gridview button android-layout focus

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

Focus in GridView layout

我使用 LayoutInflater 在 BaseAdapter 的代码中将按钮动态设置为 GridView。在代码中,我设置了按钮的文本、图像 (CompoundDrawable) 和背景颜色,因为我的按钮像复选框一样工作(蓝色背景表示已选中)。

程序似乎关注的是网格字段而不是按钮本身。我的意思是像表格的单元格而不是表格中的按钮。因为焦点颜色是默认的(绿色),所以我在选择器中设置了蓝色。焦点新闻也不起作用。焦点显然在按钮后面,在按钮的边界之外。有人可以帮我解决这个麻烦吗?我的 XML 布局代码:

主.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 中设置焦点参数并尝试了很多东西,但不幸的是它仍然不起作用:(

最佳答案

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

关于android - 焦点在 GridView 布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5683872/

相关文章:

android - AppCompatActivity openOptionsMenu 不工作

html - 我的 HTML 按钮不工作

安卓 2.3 : Read-Only file system stuck

c# - 删除 Windows 窗体中的 GridView 行

android - 将 Uris 数组添加到 GridView 中

c# - WinRT 中带包装的 GridView 方向

java - 如何向 x 次 TCP 客户端请求 TCP 服务器数据?

android - 如何使用SimpleAdapter.ViewBinder?

android - 在协程上下文中返回 Kotlin 结果会引发异常

css - 针对特定的超链接并将其变成按钮