开发者

Focus in GridView layout

开发者 https://www.devze.com 2023-02-25 00:33 出处:网络
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.

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 i开发者_运维百科t still doesn't work :(


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消