我想動態地添加文字瀏覽內LinearLayout
。一旦所有文字瀏覽的寬度都大於屏幕寬度,我需要在第一個文字的下方創建另一個LinearLayout
並繼續添加文字瀏覽。我基本上是將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>
你可以粘貼你的其他代碼嗎?的onCreate()? – AndyFaizan
@AndyFaizan確定我已經發布了 – Samantha
請發佈xml文件(佈局) –