Android:如何在网格视图中更改simple_list_item_1

时间:2015-10-15 10:10:46

标签: android

我正在尝试使用自定义布局文件替换" simple_list_item_1" 文件。但它显示错误。如何在网格视图中更改simple_list_item_1?

public class TestNumbers extends Activity {

        GridView gridView;

        static final String[] numbers = new String[] {
                "a", "b", "b", "d", "e",
              };

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

            setContentView(R.layout.number);

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

             ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, numbers){
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            String value = getItem(position);
            if (value.equals("a")) {
                view.setBackgroundColor(Color.GRAY);
            } else {
                view.setBackgroundColor(Color.RED);
            }
            // Updating the text color.
            TextView textView= (TextView) view;
            textView.setTextColor(Color.BLACK);
            return view;
        }
    };

            gridView.setAdapter(adapter);

            gridView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v,
                                        int position, long id) {

                    Toast.makeText(getApplicationContext(),
                            ((TextView) v).getText(), Toast.LENGTH_SHORT).show();


                }
            });

我刚刚添加了以下代码,并且能够更改背景颜色。如何在下面的代码中改变形状和单元格距离?

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, numbers){
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                String value = getItem(position);
                if (value.equals("a")) {
                    view.setBackgroundColor(Color.GRAY);
                } else {
                    view.setBackgroundColor(Color.RED);
                }
                // Updating the text color.
                TextView textView= (TextView) view;
                textView.setTextColor(Color.BLACK);
                return view;
            }
        };

2 个答案:

答案 0 :(得分:0)

您必须创建自定义Adapter。您在documentation

中有一个示例

答案 1 :(得分:0)

您提供给适配器的布局父级必须为<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" > </TextView> 。因此,用

替换列表项布局
$(".node text").mouseenter(function(){
  $(this).attr("fill","transparent");
}).mouseleave(function(){
  // YOUR TEXT COLOR HERE
  $(this).attr("fill","#000");
  // if you want it to default :
  //$(this).attr("fill","");
});