【问题标题】:How to set TextView width for n number of TextView in a row dynamically of ListView in android?如何在android中动态设置ListView的一行中n个TextView的TextView宽度?
【发布时间】:2016-01-19 12:53:18
【问题描述】:

我想创建一个Listview,它将水平容纳1到'n'个TextView数据,这个Textview数据和宽度大小将来自DB? 不知道怎么用holder动态设置TextView的宽度?

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    ViewHolder holder  = new ViewHolder();

    if (view == null) {
        view = View.inflate(cntxts, R.layout.list_header_col, null);
        String [] ColNmae= (String[]) objects.get(0);

        int Width= Integer.parseInt(ColNmae[2].toString());
        LinearLayout layout=new LinearLayout(cntxts);
       // layout.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        TextView Textview = new TextView(cntxts);
        HorizontalScrollView hr = new HorizontalScrollView(cntxts);
        for(int i=0;i<objects.size();i++) {
            holder.Textview=new TextView(cntxts);
            layout.setLayoutParams(new RelativeLayout.LayoutParams(Width,80));
            layout.addView(holder.Textview);
        }
        hr.addView(layout);
        view = hr;
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    notifyDataSetChanged();
    final String[] objMain = (String[]) getItem(position);
        holder.Textview.setText(objMain[0]);
    return view;
}

【问题讨论】:

  • 您将不得不使用适配器作为列表。它的基本Android101。看看:stackoverflow.com/questions/16333754/…
  • 我正在使用适配器,我想在 Listview Row 中作为 ....Col1、Col2、Col3、Col4 ......并且它的数据在下面的行中。

标签: android listview textview


【解决方案1】:
Thanks for your help I have got the solution.

 try {
                LayoutInflater mInflater = (LayoutInflater)
                        cntxts.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = mInflater.inflate(R.layout.list_header_col, null);

                holder.layoutmain = (LinearLayout) view.findViewById(R.id.lyt_header);

                view.setTag(holder);

                String[] ColNmae = (String[]) objects.get(0);

                int Width = Integer.parseInt(ColNmae[2].toString());
                if (holder.layoutmain != null && holder.layoutmain.getChildCount() > 0)
                    holder.layoutmain.removeAllViews();

                for (int i = 0; i < objects.size(); i++) {
                    final String[] objMain = (String[]) getItem(i);
                    TextView textView = new TextView(cntxts);
                    textView.setText(objMain[0]);
                    holder.layoutmain.addView(textView);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

【讨论】:

    【解决方案2】:

    您的数据需要来自某个地方。要实现 Spinner,您有以下选择:

    • 将值放入布局文件中
    • 输入代码
      • 建立自己的价值观
      • 从数据库中检索它

    【讨论】:

      【解决方案3】:

      试试这个,

       yourholder.textView.getLayoutParams().width = dynamic width value;
      

      【讨论】:

      • 谢谢!!!...它对我不起作用,我是否也必须为适配器动态创建 Textview。我没有得到它...
      • 嗨,我已经添加了我的代码,我是 android 新手。所以请纠正我。
      猜你喜欢
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      相关资源
      最近更新 更多