2017-03-16 51 views
0

如何在運行時以多行和多列創建多個文本視圖?我已經誇大了線性佈局,並使用for循環創建了文本視圖。文本視圖已成功創建,但我面臨的問題是所有創建的文本視圖都只在單行中。我試圖用LayoutParams也設置它,但不能修復它。如何解決這個問題?下面是我的代碼運行時的Android文本視圖 - 文本視圖充氣爲多行

LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

    appointmentSlotList = appointmentSlot.getAppointmentSlots(); 
    if(appointmentSlotList != null && appointmentSlotList.size()>0){ 
     for(int i = 0; i<appointmentSlotList.size(); i++){ 
      View appointmentInflater = layoutInflater.inflate(R.layout.appointment_time, null); 


      TextView lblDate = (TextView) appointmentInflater.findViewById(R.id.appointmentTime); 
      lblDate.setText(appointmentSlotList.get(i)); 
      lblDate.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
      //lblDate.setLayoutParams(lparams); 
      lblDate.setOnClickListener(onclickTime); 
      try { 
       //if(previousSelected!=i) 
       lnrDateContainer.addView(appointmentInflater); 


      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    } 

和我下面的輸出顯示:

Output for the inflated text views shown below the date picker

+0

膨脹的LinearLayout父的LinearLayout內部和內部的膨脹textviews在一個循環,這樣就可以模擬狀結構 – Nilabja

+0

表指定方位的LinearLayout –

回答

0

設置TextView的Android的佈局重心,以填補

android:layout_gravity="fill" 

如果上述不會工作,然後參考問題:Android multi line linearlayout

0

爲了實現這些我想的FlowLayout庫可能是適合您的需求。 使用這個庫,你可以在一行中創建你的TextView,當沒有空間時,自動在下一行插入視圖。

鏈接:https://github.com/ApmeM/android-flowlayout

我希望可以幫助你!

0

指定的方位你Linear Layout作爲VERTICAL

<LinearLayout 
---- 
android:orientation="vertical" 
---- />