2016-09-17 84 views
0

我有一个水平linearLayoutframeLayout。许多textViews将动态添加到linearLayout。下面是XML文件:水平linearLayout的孩子在屏幕外

<LinearLayout 
         android:id="@+id/an_item_tags_lay" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_gravity="bottom" 
         android:layout_marginBottom="20dp" 
         android:orientation="horizontal"> 

        </LinearLayout> 

和功能添加textViews是:

private void createTags(){ 

ArrayList<String> arr_tags = new ArrayList<>(); 
arr_tags.add("#flower"); 
arr_tags.add("#red"); 
arr_tags.add("#flower"); 
arr_tags.add("#red"); 
arr_tags.add("#flower"); 
arr_tags.add("#red"); 
arr_tags.add("#flower"); 
arr_tags.add("#red"); 
arr_tags.add("#flower"); 
arr_tags.add("#red"); 
arr_tags.add("#flower"); 
arr_tags.add("#red"); 

for(int i = 0; i < arr_tags.size(); i++){ 

    TextView tag = new TextView(getActivity()); 
    tag.setId(i); 
    tag.setText(arr_tags.get(i)); 
    tag.setTextColor(r.getColor(R.color.color_an_item_btn_tag_darker_color)); 
    tag.setBackgroundResource(R.drawable.btn_tag_select); 
    tag.setPadding(20, 20, 20, 20); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    params.setMargins(4, 10, 4, 4); 
    tag.setLayoutParams(params); 

    an_item_tags_lay.addView(tag); 

} 

} 

的结果如下图: enter image description here

,你可以在图片中看到,textViews会熄灭屏幕。我希望当屏幕已满时,其他视图会出现在下一行。我应该添加什么属性到linearLayout?有没有这种行为view

+0

为什么你没有固定数量的textViews在一排?说每行5,你需要有1垂直和许多水平线性视图,因为你想容纳textViews –

回答

0

你需要添加它编程方式是这样的:

tagViews.removeAllViews(); 
          LinearLayout ll = new LinearLayout(this); 
          ll.setOrientation(LinearLayout.HORIZONTAL); 
          ll.setBackgroundColor(Color.WHITE); 
          ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
          LinearLayout childView; 
          int remainingWidth, count = tagViews.getChildCount(); 
          if (count <= 0) 
           tagViews.addView(ll); 
          TextView tagText; 

          LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
            LinearLayout.LayoutParams.WRAP_CONTENT); 
          params.setMargins(5, 5, 5, 5); 

          int width = 0; 
          WindowManager w = getWindowManager(); 
          Point size = new Point(); 
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 
           w.getDefaultDisplay().getSize(size); 
           width = size.x; 
          } else { 
//   Display d = w.getDefaultDisplay(); 
//   width = d.getWidth(); 
           DisplayMetrics displaymetrics = new DisplayMetrics(); 
           getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
           width = displaymetrics.widthPixels; 
           //int screenHeight = displaymetrics.heightPixels; 
          } 
          try { 
           for (int k = 0; k < arr.length(); k++) { 
            JSONObject obj = arr.getJSONObject(k); 
            MyProductsCatTagHandler model = new MyProductsCatTagHandler(); 
            model.setName(obj.getString("category_name")); 
            model.setId(obj.getString("cat_id")); 
            tagModels.add(model); 
            final String cat_name = obj.getString("category_name"); 
            final String cat_id = obj.getString("cat_id"); 
            for (int i = 0; i < tagViews.getChildCount(); i++) { 
             childView = (LinearLayout) tagViews.getChildAt(i); 
             tagText = new TextView(this); 
             CommonUtilities.setTypeface(this, tagText, CommonUtilities.VIEW_TYPE.TEXTVIEW, CommonUtilities.TYPE_FACE.CALLIBRI, 0); 
             tagText.setText(cat_name); 
             tagText.setLayoutParams(params); 
             tagText.setPadding(10, 5, 10, 5); 
             //tagText.setBackgroundColor(Color.rgb(0xee, 0xee, 0xee)); 
             tagText.setBackgroundResource(R.drawable.tags_bg); 
             // count = holder.tagViews.getChildCount(); 
             childView.measure(0, 0); 
             remainingWidth = width - childView.getMeasuredWidth(); 
             //// Log.v("subcriptionAdapter", "remaining width=" + remainingWidth + " childview width= " + childView.getMeasuredWidth()); 
             try { 
              if (remainingWidth > childView.getMeasuredWidth()) { 
               childView.addView(tagText); 
               childView.invalidate(); 
               break; 
              } else if ((i == tagViews.getChildCount() - 1) && (remainingWidth < childView.getMeasuredWidth())) { 
               childView = new LinearLayout(this); 
               childView.setOrientation(LinearLayout.HORIZONTAL); 
               childView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
               childView.addView(tagText); 
               tagViews.addView(childView); 
               childView.invalidate(); 
               break; 
              } else { 
               continue; 
              } 
             } catch (Exception e) { 
              e.printStackTrace(); 
             } 
            } 
           } 
          } catch (Exception e) { 
           // TODO: handle exception 
           e.printStackTrace(); 
          } 
          tagViews.invalidate(); 
         } 
+0

这不会带来文本浏览到下一行作为Shima Erfan要 –

+0

谢谢@萨缪尔罗伯特我相应地编辑我的答案 –

0

你想显示在一个行或多个元素只有一个元素?

如果只有一个元素,那么你可以使用Listview。否则,您可以根据需要使用[Table layout] [2]或Grid View

希望这会有所帮助。

+0

我想多个元素,就像屏幕可以显示的一样多。 –

+2

@ShimaErfan谷歌的Android流布局,有很多 – pskink

+0

@pskink提出了一个非常好的解决方案。谢谢。 – Abhishek

相关问题