动态创建的线性布局不会包装内容

时间:2014-02-22 04:10:17

标签: android android-layout textview

我正在尝试在LinearLayout内动态添加文字视图。一旦所有textviews的所有宽度都大于屏幕的宽度,我需要在第一个下面创建另一个LinearLayout并继续添加textview。我基本上将String分成多个文本视图,因为我需要检查一些单词是否在前面有'#'。如果是这样,我需要改变颜色。我遇到的问题是第一个LinearLayout非常高(高度不包含内容)。有什么帮助吗?

LinC是linearLayout计数器。一旦textviews的宽度大于屏幕

,它就会增加
        public void TextMod()
{
    LinearLayout.LayoutParams layoutParams,layoutParams2;
    layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    llay= new LinearLayout(this.c);
    llay.setOrientation(LinearLayout.HORIZONTAL);
    llay.setBackgroundColor(Color.GREEN);
    llay2= new LinearLayout(this.c);
    llay2.setBackgroundColor(Color.MAGENTA);
    llay2.setOrientation(LinearLayout.HORIZONTAL);
    Boolean addl =true;
        String parts[]= this.Dis.split(" ");
        size=parts.length;
        int i=0;
        while( i<size)
        {
            //
            if(textview_wid < this.w )
            {
                TextView valueTV = new  TextView(c);
                String d= parts[i] + " ";
                valueTV.setText(d);
                valueTV.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);      
                 textview_wid +=valueTV.getMeasuredWidth();
                 Log.d(" des", " TEEXT WIDTH " +  valueTV.getMeasuredWidth());
                if(d.charAt(0)=='#') 
                {
                    valueTV.setTextColor(Color.MAGENTA);
                }
                if(addl)
                {
                     if(LinC==0)
                     {
                        llay.setLayoutParams(layoutParams);
                        this.Lin.addView(llay);
                     }
                     if(LinC==1)
                     {
                        llay2.setLayoutParams(layoutParams);
                        this.Lin.addView(llay2);
                     }



                    addl=false;

                }

                 valueTV.setLayoutParams(layoutParams);

                if(LinC==0)llay.addView(valueTV);
                if(LinC==1)llay2.addView(valueTV);
                i++;
            }
            else
            {

                Log.d(" des", " TOTAL TEXTVIE WIDE " + textview_wid);
                textview_wid =0;
                LinC++;
                addl=true;

            }


         //   valueTV.setLayoutParams(layoutParams);




        }

}

的OnCreate

            @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

     height =metrics.heightPixels;
     width=metrics.widthPixels;
    L=(LinearLayout)findViewById(R.id.ll);

    //layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    sep = new DescriptionStringSep(s,L,this,width);
    sep.TextMod();


}

XML

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"

android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >



<LinearLayout 
         android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ll"
    android:orientation="vertical"

    >



</LinearLayout>

 </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

你做错了什么

setContentView(R.layout.activity_main);

这将内容视图设置为作为参数传递的xml布局。由于您要在TextMod方法中创建动态布局,请稍后在代码中调用它,并在活动中设置结果布局。你应该这样做

onCreate(){
    ...
    LinearLayout mLayout;
    mLayout = sep.TextMod(); // make TextMod return the root view
    setContentView(mLayout);
}