【问题标题】:Android GridView Selected Item Background color change while pressing按下时Android GridView Selected Item背景颜色变化
【发布时间】:2012-08-30 01:07:18
【问题描述】:

我在我的android应用程序中有使用gridview的imageview。所以在gridview中按下图像时,背景颜色想要改变。如果我返回,它将变为正常的背景颜色。如何做到这一点?谁能帮助我解决这个问题?

这是我的代码

public class CustomGridViewExample extends Activity {

    private Integer[] mThumbIds = {
            R.drawable.android_2,
            R.drawable.android_2,
            R.drawable.android_2,
            R.drawable.android_2,
            R.drawable.android_2,
            R.drawable.android_2,
            R.drawable.android_2,
            R.drawable.android_2,
            R.drawable.android_2,
            R.drawable.android_2,


            };


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new MyAdapter(this));
        gridview.setNumColumns(4);
    }

  public class MyAdapter extends BaseAdapter {

        private Context mContext;

        public MyAdapter(Context c) {
            mContext = c;
        }

        @Override
        public int getCount() {
            return mThumbIds.length;
        }

        @Override
        public Object getItem(int arg0) {
            return mThumbIds[arg0];
        }

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

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

            View grid;

            if(convertView==null){
                grid = new View(mContext);
                LayoutInflater inflater=getLayoutInflater();
                grid=inflater.inflate(R.layout.mygrid_layout, parent, false);
            }else{
                grid = (View)convertView;
            }

            ImageView imageView = (ImageView)grid.findViewById(R.id.image);
            imageView.setImageResource(mThumbIds[position]);

            return grid;
        }

    }

相关的XML文件如下:

<GridView 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center"
    android:scrollbars="none" />

mygrid_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/customshape_header"
    android:orientation="vertical">
    <ImageView 
        android:id="@+id/image" 
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"/>



</RelativeLayout>

【问题讨论】:

    标签: android gridview background imageview background-color


    【解决方案1】:

    在您的 Drawable 文件夹中创建 grid_selector.xml。 然后编辑以下内容::`

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@color/green" android:state_pressed="true"/>
        <item android:drawable="@color/blue" android:state_selected="true"/>
        <item android:drawable="@color/white"/>
    
    </selector>`
    

    然后在你的 mygrid_layout.xml 文件中将此文件设置为相对布局中的背景..

    【讨论】:

      【解决方案2】:

      在您的 mygrid_layout.xml 中,使用选择器以您所需的颜色作为背景。

      【讨论】:

      • 我应该使用带有背景的选择器 ?main.xml 或 mygrid_layout.xml
      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 2021-05-28
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      相关资源
      最近更新 更多