我對此有點困惑。我有一個應用程序,用戶在其中繪製矩形文本對象。我在xml中的相對佈局中添加了一些textviews(可以說我最多有3個文本對象)。對象可以移動和調整大小。我添加此代碼爲每個TextView的android - LayoutParams如何工作?
TextView tv=(TextView) findViewById(R.id.text1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
System.out.println(texts.get(i).Sx+ " , "+texts.get(i).Sy+ " , "+ texts.get(i).Lx+ " , "+texts.get(i).Ly);
if(texts.get(i).Sx<=texts.get(i).Lx){
if(texts.get(i).Sy<=texts.get(i).Ly){
lp.setMargins((int)texts.get(i).Sx, (int)texts.get(i).Sy, (int)texts.get(i).Lx, (int)texts.get(i).Ly);
} else{
lp.setMargins((int)texts.get(i).Sx, (int)texts.get(i).Ly, (int)texts.get(i).Lx, (int)texts.get(i).Sy);
}
} else{
if(texts.get(i).Sy<=texts.get(i).Ly){
lp.setMargins((int)texts.get(i).Lx, (int)texts.get(i).Sy, (int)texts.get(i).Sx, (int)texts.get(i).Ly);
} else{
lp.setMargins((int)texts.get(i).Lx, (int)texts.get(i).Ly, (int)texts.get(i).Sx, (int)texts.get(i).Sy);
}
}
tv.setLayoutParams(lp);
tv.setWidth((int)(texts.get(i).width));
tv.setHeight((int)(texts.get(i).height));
tv.setTextSize(texts.get(i).textSize);
tv.setText(text.text);
tv.setTextColor(Color.WHITE);
tv.requestLayout();
SX,SY是對象的以像素爲單位和LX,Ly的結束庫斯起始庫斯。
在XML中添加此:
<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/texts" >
<TextView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/text1" />
<TextView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/text2" />
<TextView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/text3" />
</RelativeLayout>
這似乎在將文本放置在正確的位置爲工作。但是textview的寬度和高度似乎並不正確。這是以像素爲單位設置邊距的問題嗎?一些啓發現在將非常有用
如果您@ text1的垂直和水平填充母,再沒有什麼留給其他 – max4ever
它是一個RelativeLayout的,使他們能夠在另一個之上是嗎?問題是,只有text1它也有問題 – george