2016-06-07 103 views
-3

我在線性佈局(lay)內創建各種textviews。我希望當textview伸出屏幕時,那個文本視圖會自動跳到下一行,而不是跳出屏幕。我是一個Android的初學者,並沒有得到如何實現它。如何在android中動態創建textviews?

這裏是我的代碼:

for (int i = 0; i < string.size(); i++) { 
     final TextView txt = new TextView(con); 
     txt.setText(string.get(i)); 
     lay.addView(txt); 
     } 
+0

LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ); tv.setLayoutParams(lp); –

+0

對不起@Jitendraramoliya它不工作在我的情況。 – Shubh

+0

什麼是您在textView中設置的文字? –

回答

0

嘗試這個

TextView txt = new TextView(context); 
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
+0

對不起@mdDroid它不工作在我的情況 – Shubh

0
private void populateViews(LinearLayout linearLayout, ArrayList<TextView> views, View extraView) 
{ 
    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    float mWidth = displaymetrics.widthPixels; 
    extraView.measure(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

    linearLayout.removeAllViews(); 
    int maxWidth = (int) mWidth - extraView.getMeasuredWidth() - 10; 

    linearLayout.setOrientation(LinearLayout.VERTICAL); 

    LinearLayout.LayoutParams params; 
    LinearLayout newLL = new LinearLayout(mContext); 
    newLL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
    newLL.setGravity(Gravity.LEFT); 
    newLL.setOrientation(LinearLayout.HORIZONTAL); 

    int widthSoFar = 0; 

    for (int i = 0; i < views.size(); i++) 
    { 
     LinearLayout LL = new LinearLayout(mContext); 
     LL.setOrientation(LinearLayout.HORIZONTAL); 
     LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM); 
     LL.setLayoutParams(new ListView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     views.get(i).measure(0, 0); 
     params = new LinearLayout.LayoutParams(views.get(i).getMeasuredWidth(), LinearLayout.LayoutParams.WRAP_CONTENT); 
     params.setMargins(10, 2, 10, 2); 

     LL.addView(views.get(i), params); 
     LL.measure(0, 0); 
     widthSoFar += views.get(i).getMeasuredWidth(); 
     if (widthSoFar >= maxWidth) 
     { 
      linearLayout.addView(newLL); 

      newLL = new LinearLayout(mContext); 
      newLL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
      newLL.setOrientation(LinearLayout.HORIZONTAL); 
      newLL.setGravity(Gravity.LEFT); 
      params = new LinearLayout.LayoutParams(LL.getMeasuredWidth(), LL.getMeasuredHeight()); 
      newLL.addView(LL, params); 
      widthSoFar = LL.getMeasuredWidth(); 
     } 
     else 
     { 
      newLL.addView(LL); 
     } 
    } 
    linearLayout.addView(newLL); 
} 

這種方法應該能夠幫助你。我早就把這個方法做了很久,所以經過一些修改,你應該可以使用它。第一個參數是LinearLayout,它將成爲基本的垂直線性佈局,它將充當視圖的主要容器。第二個參數是您必須添加的實際視圖。在我的情況下,第三個參數是一個像佔位符一樣使用的圖像。

0

你也可以這樣添加。

 for (int i = 0; i < string.size(); i++) { 
      LayoutInflater inflater = (LayoutInflater) mContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View view = (View) inflater.inflate(
        R.layout.Layout_Design, null); 
      TextView txt = (TextView) view.findViewById(R.id.some_text); 
      txt.setText("for runtime change"); 
      layout.addView(view); 
      // layout is linear layout in my case. 
     } 

Layout_Design:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="80dp" 
    android:layout_height="80dp" > 

    <RelativeLayout 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:paddingBottom="5dp" 
     android:paddingLeft="5dp" 
     android:paddingRight="0dp" 
     android:paddingTop="5dp" > 

     <TextView 
      android:id="@+id/some_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="SOme Text" /> 
    </RelativeLayout> 

</FrameLayout>