如何更改Android库选择的项目颜色?

时间:2015-06-01 07:37:54

标签: android android-gallery

我正在尝试编写一个代码来显示带有无限列表的Android图库视图。我能够在屏幕上正确显示图库,但我想更改图库中所选项目的颜色。

mycode的:

MainActivity.java

package com.example.mycircularlistview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Gallery;

public class MainActivity extends Activity {

String[] txtData;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Gallery gallery = (Gallery) findViewById(R.id.mygallery);


        txtData = new String[12];

        for(int i=0;i<txtData.length;i++){

            txtData[i] = ""+i;
        }

        gallery.setAdapter(new GalleryAdapter(this,txtData));

        gallery.setSpacing(18);


}


}

GalleryAdapter.java

package com.example.mycircularlistview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class GalleryAdapter extends BaseAdapter {

    private LayoutInflater inflater = null;

    String[] txtData;

    public GalleryAdapter(Context c, String[] txtData) {

        inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.txtData = txtData;
    }

    public int getCount() {

        if (txtData != null) {

            return Integer.MAX_VALUE;

        } else {

            return 0;

        }

    }

    public Object getItem(int position) {

        if(position >= txtData.length) {

            position = position % txtData.length;

        }

        return position;
    }

    public long getItemId(int position) {

        if(position >= txtData.length) {

            position = position % txtData.length;

        }

        return position;
    }

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

        View view = convertView;

        TextView txt = null;

        view = inflater.inflate(R.layout.test_layout, parent, false);

        txt = (TextView) view.findViewById(R.id.txtImg); 


        if(position >= txtData.length) {
            position = position % txtData.length;
        }


        txt.setText(""+txtData[position]+" :: "+position);


        return view;

    }
}

activity_main.xml中

 <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout1">

    <Gallery
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mygallery"
        android:layout_weight="1"/>

</LinearLayout>

屏幕截图:

enter image description here

这些是我的问题的所有代码和屏幕截图。所以,你可以查看屏幕截图,屏幕上一直有一组5个元素,所以我想改变中间索引的颜色。

请建议我一些解决方案。

0 个答案:

没有答案