android 绝对布局被放弃,android绝对布局
好吧,整整24小时的头疼我终于想出了一个答案为我自己和其他人:D 感谢大家,您的投入肯定会让我走向正确的方向!
基本上framelayout边缘不能正常工作,除非我失去了一些东西。所以我所做的是必须扩展一个完全展开的线性布局,并在其内部保留所需的视图。
例如:
public class BrPositionableTextView extends LinearLayout{
TextView text ;
public BrPositionableTextView(Context context, int x, int y, int w, int h) {
super(context);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
text = new TextView(context);
text.setHeight(h);
text.setGravity(Gravity.CENTER_VERTICAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(x, y, 0, 0);
params.height = h;
params.width = w;
addView(text, params);
}
/**
* @return the text
*/
public String getText() {
return text.getText().toString();
}
/**
* @param text the text to set
*/
public void setText(String text) {
this.text.setText(text);
}
public void setMargins(int x, int y, int w, int h) {
//not tested, may need to recreate textview…
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(x, y, x+w, y+h);
text.setLayoutParams(params);
}
public void setBackgroundColor(int color)
{
text.setBackgroundColor(color);
}
}
这可以随后被添加到的FrameLayout并显示在正确的位置的图。
例子:
ScrollView dayViewContainer = new ScrollView(context);
dayViewContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
FrameLayout dayView = new FrameLayout(context);
dayView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
dayView.setBackgroundColor(0xFF005000);
dayView.setMinimumHeight(BrScreenMetrics.getScreenHeightMinusStatusBar());
dayView.setMinimumWidth(BrScreenMetrics.getScreenWidthPX());
int lineHeight = 60;
for(int i =0;i<11 ;i++){
BrPositionableTextView hourView = new BrPositionableTextView(
context,
8,
i*lineHeight,
80,
lineHeight);
hourView.setText(“”+(i+7)+”:00”);
dayView.addView(hourView);
BrPositionableSeparator separator = new BrPositionableSeparator(
context,
80,
(i*lineHeight)+((int)(lineHeight*0.5f)),
BrScreenMetrics.getScreenWidthPX()-117,
1);
separator.setColor(0xFFFFFFFF);
dayView.addView(separator);
}
BrPositionableTextView bigassBox= new BrPositionableTextView(context, 100,200,300,500);
bigassBox.setBackgroundColor(0xFF0000FF);
dayView.addView(bigassBox);
dayViewContainer.addView(dayView);
addView(dayViewContainer);
}
还没有评论,来说两句吧...