如何在Android中将TextView添加到GridView中 [英] How to add a TextView to a GridView in Android

查看:83
本文介绍了如何在Android中将TextView添加到GridView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经知道如何将String添加到android的gridView中,但是我想添加textviews来格式化文本(重力,颜色等)。这是我目前的代码:

  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?

解决方案

使用类似的方法尝试它
$ b $ p gridview.xml

 <?xml version =1.0encoding =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 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);

查看gridView;

if(convertView == null){

gridView = new View(context);

//从mobile.xml获取布局
gridView = inflater.inflate(R.layout.item,null);

//将值设置为textview
TextView textView =(TextView)gridView
.findViewById(R.id.grid_item_label);
textView.setText(textViewValues [position]);
} else {
gridView =(View)convertView;
}

返回gridView;
}

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

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

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

}

然后,最后设置适配器您的主要类



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

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

  textView.setColor(textViewColors [position]); 

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


I already know how to add String's to a gridView in android however I want to add textviews to format the text( Gravity, color, etc). This is my current code:

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

Unfortunately when I run it the gridview simply gives me the information of each TextView: "android.id.widget.TextView({42f20a88 and so on}.

How would I add textview's to a gridView?

解决方案

try it with something like this

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>

Custom adapter class for 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;
        }

    }

and then , finally set adapter in your main class

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

also note that you can pass any other values like color of text and as another argument in constructor and then set them in adapter like ...

textView.setColor(textViewColors[position]); 

for attributes that all textview have in common you can change the item.xml only :) i hope it helped you ....

这篇关于如何在Android中将TextView添加到GridView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆