2017-03-16 50 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" 
---- />