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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值