【问题标题】:How to add a TextView to a GridView in Android如何在 Android 中将 TextView 添加到 GridView
【发布时间】:2014-03-15 21:15:00
【问题描述】:

我已经知道如何在 android 中将字符串添加到 gridView,但是我想添加 textview 来格式化文本(重力、颜色等)。这是我当前的代码:

GridView gridView = (GridView) findViewById(R.id.grades);
    ArrayList<TextView> display = new ArrayList<TextView>();
    ArrayAdapter<TextView> adapter = new ArrayAdapter<TextView>(this,
            android.R.layout.simple_list_item_1, display);

    gridView.setAdapter(adapter);
    for(int i = 0; i<currentClass.grades.size(); i++)
    {
        TextView name = new TextView(this);
        TextView weight = new TextView(this);
        TextView cur_points = new TextView(this);
        TextView total_points = new TextView(this);
        TextView cur_percent = new TextView(this);

        name.setText(currentClass.grades.get(i).name);
        weight.setText(currentClass.grades.get(i).weight);
        cur_points.setText(currentClass.grades.get(i).cur_points);
        total_points.setText(currentClass.grades.get(i).total_points);
        cur_percent.setText(currentClass.grades.get(i).cur_percent);

        name.setTextColor(Color.WHITE);
        weight.setTextColor(Color.WHITE);
        cur_points.setTextColor(Color.WHITE);
        total_points.setTextColor(Color.WHITE);
        cur_percent.setTextColor(Color.WHITE);

        name.setSingleLine(true);
        weight.setSingleLine(true);
        cur_points.setSingleLine(true);
        total_points.setSingleLine(true);
        cur_percent.setSingleLine(true);

        name.setGravity(Gravity.LEFT);
        weight.setGravity(Gravity.RIGHT);
        cur_points.setGravity(Gravity.RIGHT);
        total_points.setGravity(Gravity.RIGHT);
        cur_percent.setGravity(Gravity.RIGHT);

        display.add(name);
        display.add(weight);
        display.add(cur_points);
        display.add(total_points);
        display.add(cur_percent);

        adapter.notifyDataSetChanged();
    }

不幸的是,当我运行它时,gridview 只是给了我每个 TextView 的信息: "android.id.widget.TextView({42f20a88 等}.

如何将 textview 添加到 gridView?

【问题讨论】:

  • 创建自定义适配器可能会更好。我相信您的版本无法正常工作的原因是 ArrayAdapter 和您正在使用的参数。

标签: android gridview


【解决方案1】:

试试这样的

gridview.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</GridView>

item.xml

<TextView
    android:id="@+id/grid_item_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:layout_marginTop="5px"
    android:textSize="15px" >
</TextView>

textView 的自定义适配器类

public class TextViewAdapter extends BaseAdapter {
        private Context context;
        private final String[] textViewValues;

        public TextViewAdapter(Context context, String[] textViewValues) {
            this.context = context;
            this.textViewValues = textViewValues;
        }

        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);

                // get layout from mobile.xml
                gridView = inflater.inflate(R.layout.item, null);

                // set value into textview
                TextView textView = (TextView) gridView
                        .findViewById(R.id.grid_item_label);
                textView.setText(textViewValues[position]);  
            } else {
                gridView = (View) convertView;
            }

            return gridView;
        }

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

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

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

    }

然后,最后在你的主类中设置适配器

gridView.setAdapter(new TextViewAdapter(this, YOUR_ARRAY_WITH_TEXT_FOR_TEXTVIEWS));

还请注意,您可以传递任何其他值,例如文本颜色和构造函数中的另一个参数,然后将它们设置在适配器中,例如 ...

textView.setColor(textViewColors[position]); 

对于所有 textview 共有的属性,您只能更改 item.xml :) 希望对你有所帮助....

【讨论】:

    【解决方案2】:

    只需执行 name.getText() 即可!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多