【问题标题】:Custom grid view自定义网格视图
【发布时间】:2014-07-05 13:57:18
【问题描述】:

这是我的代码:

public class CustomGridAdapter extends BaseAdapter{

Context ctx;
String [] items ={"72","58","67","77","90"} ; //Can be images or video or any other content
String [] stuff = {"bus to town ","Bus to north","Bus to west","Bus to east","Bus out"};
public CustomGridAdapter(Context c) {
    ctx = c;
}

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

@Override
public Object getItem(int position) {
    return items[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView tv = new TextView(ctx);
    TextView tv2 = new TextView(ctx);
    tv.setText(String.valueOf(items[position]));
    tv2.setText(String.valueOf(stuff[position]));
    return tv;
}

}

需要帮助试图弄清楚如何在 tv 下方显示 tv2 。 有什么建议吗?

【问题讨论】:

  • 你为什么不在getView(....)方法中创建一个布局xml并膨胀这些xml?

标签: java android android-layout android-activity android-gridview


【解决方案1】:

你需要创建一个垂直的LinearLayout:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout layout = new LinearLayout(ctx);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    TextView tv = new TextView(ctx);
    TextView tv2 = new TextView(ctx);
    tv.setLayoutParams(lparams);
    tv2.setLayoutParams(lparams);
    tv.setText(String.valueOf(items[position]));
    tv2.setText(String.valueOf(stuff[position]));
    layout.addView(tv);
    layout.addView(tv2);
    return layout;
}

【讨论】:

  • LinearLayout layout = new LinearLayout(this);,this 对如何使其工作没有任何建议?
  • 构造函数 LinearLayout(CustomGridAdapter) 未定义,正如它所说的
  • 我修正了错字,将其替换为 ctx
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 2014-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多