tableLayout 实现类似gridview的效果 带分割线

  1. /** 
  2.      * 设置表格的数据 
  3.      * @param tableDatas 
  4.      */  
  5.     public void setTableLayout(List<HashMap<String, Object>> tableDatas) {  
  6.         TableLayout mainLayout = new TableLayout(getContext());  
  7.         LinearLayout rowLayout = null;  
  8.         for (int i = 0; i < tableDatas.size(); i++) {  
  9.             if (i % 4 == 0) {  
  10.                 if(i != 0) {  
  11.                     setHorizontalSplit(mainLayout);  
  12.                 }  
  13.                 rowLayout = getRowLayout(mainLayout);  
  14.             }  
  15.             setItemLayout(i, 4, rowLayout, tableDatas,View.VISIBLE);  
  16.         }  
  17.         if(tableDatas.size()%4 != 0) {  
  18.         for(int i = 0; i < (4 -tableDatas.size()%4); i++) {  
  19.                 setItemLayout(i+34, rowLayout, tableDatas,View.INVISIBLE);  
  20.             }  
  21.         }  
  22.         setCustomView(mainLayout);  
  23.     }  
  24.       
  25.     /** 
  26.      * 设置水平分割线 
  27.      * @param mainLayout 
  28.      */  
  29.     private void setHorizontalSplit(LinearLayout mainLayout) {  
  30.         ImageView view = new ImageView(getContext());  
  31.         android.view.ViewGroup.LayoutParams params = new android.view.ViewGroup.LayoutParams(  
  32.                 android.view.ViewGroup.LayoutParams.MATCH_PARENT, 2);  
  33.         view.setBackgroundColor(Color.rgb(227227227));  
  34.         mainLayout.addView(view, params);  
  35.     }  
  36.       
  37.     /** 
  38.      * 获取rowLayout 
  39.      * @param mainLayout 
  40.      * @return 
  41.      */  
  42.     private LinearLayout getRowLayout(TableLayout mainLayout) {  
  43.         LinearLayout rowLayout = new LinearLayout(getContext());  
  44.         rowLayout.setOrientation(LinearLayout.HORIZONTAL);  
  45.         android.view.ViewGroup.LayoutParams rowParams = new android.view.ViewGroup.LayoutParams(  
  46.                 android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);  
  47.         mainLayout.addView(rowLayout,rowParams);  
  48.           
  49.         return rowLayout;  
  50.     }  
  51.       
  52.     /** 
  53.      * 设置itemlayout 
  54.      * @param index 
  55.      * @param columnNum 
  56.      * @param rowLayout 
  57.      * @param visibility 
  58.      */  
  59.     private void setItemLayout(int index, int columnNum, LinearLayout rowLayout,   
  60.             List<HashMap<String, Object>> tableDatas, int visibility) {  
  61.         LinearLayout itemLayout = new LinearLayout(getContext());  
  62.         android.widget.LinearLayout.LayoutParams params1 = new android.widget.LinearLayout.LayoutParams(  
  63.                 android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,100f);  
  64.         itemLayout.setOrientation(LinearLayout.VERTICAL);  
  65.         itemLayout.setId(index);  
  66.         itemLayout.setBackgroundResource(R.drawable.bottom_dialog_item_selector);  
  67.         itemLayout.setOnClickListener(this);  
  68.         ImageView iv = new ImageView(getContext());   
  69.         iv.setImageResource((Integer) tableDatas.get(index).get("extra_big_icon"));  
  70.         itemLayout.addView(iv);  
  71.         TextView tv = new TextView(getContext());  
  72.         tv.setTextColor(Color.rgb(127127127));  
  73.         tv.setPadding(015015);   
  74.         tv.setGravity(Gravity.CENTER);  
  75.         tv.setText((CharSequence) tableDatas.get(index).get("extra_type"));   
  76.         itemLayout.addView(tv);  
  77.         rowLayout.addView(itemLayout,params1);  
  78.         if(index % columnNum != (columnNum - 1)) {  
  79.             ImageView view = new ImageView(getContext());  
  80.             android.view.ViewGroup.LayoutParams params = new android.view.ViewGroup.LayoutParams(  
  81.                     2, android.view.ViewGroup.LayoutParams.MATCH_PARENT);  
  82.             view.setBackgroundColor(Color.rgb(227227227));  
  83.             rowLayout.addView(view,params);  
  84.             view.setVisibility(visibility);  
  85.         }  
  86.         itemLayout.setVisibility(visibility);  

  1.     }  
  2. 这是一个dialog从底部弹出的效果:


发布了134 篇原创文章 · 获赞 26 · 访问量 77万+

猜你喜欢

转载自blog.csdn.net/yw1688/article/details/54133985
今日推荐