0

我正在开发一个带有GridView. gridView 包含按钮。我用按钮适配器自定义了我的 gridView。

问题是:我需要用边框突出显示所选项目。现在选择在释放新闻后消失。
我不是安卓专家。所以按下按钮后的下一步是显示云形状的图像,上面写着“确认”。

正是我所需要的。

4

1 回答 1

3

drawable在您的文件夹中创建一个名为的res文件夹。现在在文件夹中创建一个xml文件drawable,将其命名为任何名称(稍后)并将此代码放入文件中。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true" android:state_pressed="true"><shape>

            <!-- <solid android:color="#CCCCCC"/> -->

            <gradient android:endColor="#67A7F8" android:startColor="#1067C8" />

            <stroke android:width="1dp" android:color="#000000" />

            <corners android:radius="8dp" />
        </shape></item>
    <item android:state_focused="false" android:state_pressed="true"><shape>

            <!-- <solid android:color="#07B107"/> -->

            <gradient android:endColor="#67A7F8" android:startColor="#1067C8" />

            <stroke android:width="1dp" android:color="#000000" />

            <corners android:radius="8dp" />
        </shape></item>
    <item android:state_focused="true" android:state_pressed="false"><shape>
            <solid android:color="#FFFFFF" />

            <stroke android:width="1dp" android:color="#0055FF" />

            <corners android:radius="8dp" />
        </shape></item>
    <item android:state_focused="false" android:state_pressed="false"><shape>
            <gradient android:angle="270" android:centerColor="#FFFFFF" android:endColor="#FFFFFF" android:startColor="#F2F2F2" />

            <stroke android:width="0.8dp" android:color="#000000" />

            <corners android:radius="12dp" />
        </shape></item>
    <item android:state_enabled="true"><shape>
            <padding android:bottom="4dp" android:left="5dp" android:right="4dp" android:top="4dp" />
        </shape></item>

</selector>

现在在您的 GridView代码集 android:drawSelectorOnTop="true"android:listSelector属性中,然后选择drawable您刚刚创建的。它将解决您的问题。

您的网格视图代码将如下所示:

    <GridView
        android:id="@+id/lstFrames_available_frames"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:animateLayoutChanges="true"
        android:columnWidth="100dp"
        android:gravity="center"
        android:horizontalSpacing="3dp"
        android:listSelector="@drawable/round_buttons"
        android:numColumns="auto_fit"
        android:drawSelectorOnTop="true"
        android:stretchMode="columnWidth"
        android:verticalSpacing="3dp" >
    </GridView>

更新 您可以在自定义适配器中使用类似的东西来实现您想要的:

public class AlbumCoverAdapter extends BaseAdapter {

    private Activity activity;

    private static LayoutInflater inflater = null;

    private int mSelected;

    public AlbumCoverAdapter(Activity a) {
        activity = a;

        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return 50;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
        public TextView txtCaption;

        public ImageView imgImage;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        ViewHolder holder;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.grid_adapter, null);
            holder = new ViewHolder();
            holder.txtCaption = (TextView)vi.findViewById(R.id.txtGridText);
            holder.txtCaption.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    mSelected = (Integer)arg0.getTag();
                    notifyDataSetChanged();
                }
            });
            vi.setTag(holder);
        } else
            holder = (ViewHolder)vi.getTag();

        try {
            holder.txtCaption.setTag(position);

            if (position == mSelected) {
                holder.txtCaption.setBackgroundResource(R.drawable.round_corner_background);
            } else {
                holder.txtCaption.setBackgroundDrawable(null);

            }

            holder.txtCaption.setText("Item: " + position);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return vi;
    }

}
于 2013-04-29T11:11:25.503 回答