2014-03-06 152 views
3

我有一個佈局,我需要插入到某些類型的塊與文本 - 如評論日期,名稱,文本。有沒有辦法使用它像這樣Dynamicaly在佈局中添加布局?

for (int r=0; r<10;r++) { 
     TextView t1=(TextView) findviewbyid(R.id.text1); 
     t1="BlaBla"; 
     mainlayout.add(commentLayout); 
} 

,所以我得到像列表視圖,但沒有適配器的方式等

<RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Name" 
    android:id="@+id/comm_name" 
    android:layout_marginLeft="20dp" 
    android:layout_marginTop="24dp" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Date" 
    android:id="@+id/comm_date" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:layout_margin="20dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Text" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="60dp" 
    android:layout_marginLeft="20dp" 
    android:id="@+id/comm_text" /> 

+0

是的,這是可能的,你有問題嗎? – Devrim

回答

5

,你可以這樣做,但你正在做的方式它會拋出一個異常,因爲findViewById總是返回相同的對象,並且在第一次迭代t1已經有一個父對象。您應該在每次迭代中以編程方式創建TextView的新實例。

5

嘗試在滾動視圖內的線性佈局中動態添加視圖。

試試這個代碼。我知道它不是確切的解決方案。但是,希望它給你一些想法。

public class MainActivity extends Activity { 

    ScrollView scrollview; 
    LinearLayout linearLayout; 
    LinearLayout.LayoutParams layoutParams; 
    static int i; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     scrollview = (ScrollView)findViewById(R.id.scrollview); 
     linearLayout = (LinearLayout)findViewById(R.id.linearlayout); 
     Button button = (Button)findViewById(R.id.button); 
     layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

     button.setOnClickListener(new OnClickListener(){ 

      public void onClick(View v) { 
       TextView view = new TextView(MainActivity.this);    
       view.setText(++i+" view"); 
       linearLayout.addView(view, layoutParams); 
      } 

     }); 

}} 
1

使用下面的方法

循環動態添加視圖以線性佈局

TextView textView1, textView2; 
LinearLayout completeLinearLayout = (LinearLayout) findViewById(R.id.ll); 
for (int i = 0; i < 10; i++) { 
     textView1 = new TextView(Activity.this); 
     textView2 = new TextView(Activity.this); 
     String name1 = "xyz"; 
     String name2 = "abc" 
     completeLinearLayout.addView(textView1); 
     completeLinearLayout.addView(textView2); 
     setTextViewParam(textView1, name1); 
     setTextViewParam(textView2, name2); 

    } 

方法來設置TextView的參數進行動態

private void setTextViewParam(TextView tv, String text) { 

    LayoutParams textLayoutParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT); 
    textLayoutParams.setMargins(5, 0, 0, 0); 
    tv.setGravity(Gravity.LEFT); 
    tv.setText(text); 
    tv.setTextSize(13); 
    tv.setTextColor(Color.BLACK); 
    tv.setLayoutParams(textLayoutParams); 

} 

現在您可以根據需要和容器自定義視圖。 快樂編碼!

+0

對於任何問題問我:) –

+0

Oky ..請將您的佈局與標記並告訴我我需要設置哪些值。我會相應地更新我的答案 –

+0

已更新主文章 – SERG

-1

因此,此代碼工作exactlay,因爲我需要。 它有什麼問題嗎?

public class InflateView extends Activity { 

LinearLayout lLayout; 
TextView t,f; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    lLayout = (LinearLayout)findViewById(R.id.layout1); 

    for (int i=0;i<10;i++) 
    { 
      RelativeLayout tv = (RelativeLayout)inflater.inflate(R.layout.text, null); 
      lLayout.addView(tv); 
      t = (TextView) tv.findViewById(R.id.ttt); 
      t.setText("aaaaaaa" + i) ; 
      f = (TextView) tv.findViewById(R.id.textView); 
      f.setText(String.valueOf(i)); 
    } 
}}