2016-01-19 120 views
0

我想要創建一個ListView,它將水平保持1到'n'的TextView數據的數量,這個Textview的數據和寬度的大小將來自數據庫? 我不知道如何設置動態持有人的TextView的寬度?感謝先進。如何在android中的ListView中動態設置n個TextView的TextView寬度?

@覆蓋 公共查看getView(INT位置,查看視圖,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; 
} 
+1

你將不得不使用一個適配器列表。其基本的Android101。看看:http://stackoverflow.com/questions/16333754/how-to-customize-listview-using-baseadapter – zIronManBox

+0

我使用適配器,我想在列表視圖行.... Col1,Col2,Col3, Col4 ......及其數據在下面的行中。 –

回答

0

你的數據需要從什麼地方來。履行微調你有以下幾種選擇:

  • 把值在佈局文件
  • 代碼
    • 建立自己的價值觀
    • 從DB
    • 檢索它把
0

試試這個,

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

Thanks !!! ...它不適合我,我應該不得不動態創建Textview以及適配器。我沒有得到它... –

+0

在這裏發佈您的代碼 –

+0

嗨,我已經添加了我的代碼我是新的到android。所以請糾正我。 –

0
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(); 
      } 
相關問題