GridView项目文本不改变颜色

时间:2016-09-04 15:24:34

标签: java android xml android-gridview

我试图更改网格视图中特定项目的文本颜色,但由于某种原因它无法正常工作。每次我启动我的应用程序时,我所有gridview项目的文本颜色都保持白色,这不是我预期的结果,尽管使用了代码。有谁知道出了什么问题以及需要做些什么才能解决这个问题?

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<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">

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:listSelector="@android:color/transparent"
        android:clickable="false"
        android:numColumns="auto_fit"
        android:columnWidth="90dp"
        android:gravity="center"
        android:stretchMode="columnWidth">
    </GridView>
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    GridView gridView;

    static final String[] years = new String[]{
            "1863", "1864", "1868", "1871", "1890",
            "1898", "1900", "1906", "1968", "1979"};

    @Override
    public void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

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

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.gridview_item, R.id.item_gridview, years);
        gridView.setAdapter(adapter);
    }

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

    TextView tv = (TextView) convertView.findViewById(R.id.item_gridview);

    if(position == 4 | position == 6){
        tv.setTextColor(Color.parseColor("#1e1eff"));
    }
    else if(position == 2){
        tv.setTextColor(Color.parseColor("#e32017"));
    }
    else {
        tv.setTextColor(Color.WHITE);
    }


    return tv;
}
}

布局/ gridview_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/item_gridview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

您需要初始化布局和自定义适配器

<强>更新

public class MainActivity extends Activity {

    GridView gridView;
    public MyAdapter adapter;
    static final String[] years = new String[] {
        "1863", "1864",  "1868", "1871", "1890",
        "1898", "1900", "1906", "1968", "1979"
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        gridView = (GridView) findViewById(R.id.gridView1);
        adapter = new MyAdapter(getApplicationContext(), 0);
        gridView.setAdapter(adapter);
        for (int i = 1; i < years.length; i++) {

            adapter.addAdapterItem(new AdapterItem(years[i]));
        }
    }

    public class MyAdapter extends ArrayAdapter < AdapterItem > {
        private List < AdapterItem > items = new ArrayList < AdapterItem > ();

        // private static LayoutInflater inflater=null;
        public MyAdapter(Context context, int textviewid) {
            super(context, textviewid);
        }

        public void addAdapterItem(AdapterItem item) {
            items.add(item);
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public AdapterItem getItem(int position) {
            return ((null != items) ? items.get(position) : null);
        }

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

        @SuppressWarnings("deprecation")
        @Override
        public View getView(final int position, View convertView,
            final ViewGroup parent) {
            View rowView;
            if (convertView == null) {
                rowView = getLayoutInflater().inflate(R.layout.gridview_item,
                    null);
            } else {
                rowView = convertView;
            }
            TextView tv = (TextView) rowView.findViewById(R.id.item_gridview);
            tv.setText(items.get(position).first);
            if (position == 4 || position == 6) {

                tv.setTextColor(Color.parseColor("#1e1eff"));
            } else if (position == 2) {
                tv.setTextColor(Color.parseColor("#e32017"));
            } else {
                // tv.setTextColor(Color.WHITE);
            }

            return rowView;
        }
    }

    public class AdapterItem {
        public String first;

        // add more items
        public AdapterItem(String first) {
            this.first = first;

        }
    }
}