4

我想显示每个 gridview 项目的不同边框。如何动态设置每个项目的边框。

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

<ImageView
    android:id="@+id/grid_item_image"
    android:layout_width="30px"
    android:layout_height="30px"
    android:src="@drawable/shape" >
</ImageView>

</LinearLayout>

activity_main 布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:numColumns="15" 
    android:verticalSpacing="1dp"
    android:horizontalSpacing="1dp"
    android:background="#f3f3f3"
    >
</GridView>

</RelativeLayout>

主类:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gridView = (GridView) findViewById(R.id.gridView1);

    gridView.setAdapter(new ImageAdapter(this));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            Toast.makeText(
               getApplicationContext(),
               /*((TextView) v.findViewById(R.id.grid_item_label)).getText() + */String.valueOf(position), Toast.LENGTH_SHORT).show();
            ImageView imageView = (ImageView) v.findViewById(R.id.grid_item_image);
            imageView.setBackgroundColor(Color.BLUE);


        }
    });

适配器类:

 public class ImageAdapter extends BaseAdapter {
private Context context;

public ImageAdapter(Context context) {
    this.context = context;
}

public View getView(int position, View convertView, ViewGroup parent) {

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

    View gridView;

    if (convertView == null) {
        gridView = new View(context);
        gridView = inflater.inflate(R.layout.mobile, null);
    } else {
        gridView = (View) convertView;
    }

    return gridView;
}

@Override
public int getCount() {
    return 300;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

}

编辑

我在自定义设计文件中添加了一个形状文件

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@null" />
<stroke android:width="1dp" android:color="#000000" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</shape>
4

3 回答 3

2

使用以下代码将文件保存XML在您的Drawable文件夹中:

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

    <item android:state_enabled="true"><shape>
            <solid android:color="#FFFFFF" />

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

            <corners android:radius="5dp" />

     <!--       <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" />-->
        </shape></item>

</selector>

现在将其用作background您的一个image view并将您的 iamges 设置为background resources

于 2013-06-14T05:53:58.600 回答
1

在您的类 ImageAdapter --> getView(...) 方法中,在 return gridView;语句之前添加此行

gridView.setBackgroundColor(Color.RED);

编辑

还添加android:layout_margin="10dp"到您的图像视图中...

以此为参考..在提供的链接中,边框应用于完整的网格视图,但它给出了边框设置的想法,使用我提到的逻辑来实现这一点..

希望这可以帮助..

于 2013-06-14T04:50:41.263 回答
1
gridView = (GridView)findViewById(R.iMyGridview);
gridView.setBackgroundColor(Color.LTGRAY);
gridView.setVerticalSpacing(1);
gridView.setHorizontalSpacing(1);
于 2015-01-16T05:26:10.367 回答