专注于GridView布局

时间:2011-04-16 01:31:14

标签: android gridview button android-layout focus

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

Focus in GridView layout

我使用LayoutInflater在BaseAdapter的代码中动态地将Buttons设置为GridView。在代码中,我设置了按钮的文本,图像(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中设置焦点参数,并尝试了很多东西,但不幸的是它仍然不起作用:(

2 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:1)

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