安卓绝对布局
我有一个使用 TextViews
进行布局的复杂时间表。我发现绝对布局已被贬值。现在我知道这是“不好的做法”,但我有一个格式良好的屏幕指标类,它将调整与屏幕尺寸/密度等相关的所有内容。
如果我不能使用绝对布局,那么我如何告诉 TextView 我想要这么多左边和上面? (就像在 HTML 中使用绝对位置一样)?
我想以编程方式完成此操作,而不是使用 XML。这是可能的还是我必须使用 WebView
或画布?
编辑::
void positionView (TextView v, int x, int y, int w, int h){
final ViewGroup.MarginLayoutParams lpt = new ViewGroup.MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);//v.getLayoutParams();
//lpt.setMargins(x,y,x+w,y+h);
lpt.height=h;
lpt.width=w;
lpt.topMargin=y;
lpt.leftMargin=x;
v.setLayoutParams(lpt);
}
这是我从这个快速测试代码调用的方法:
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(0xFF00FF00)
int lineHeight = 60;
for(int i =0;i<11 ;i++){
TextView hourView = new TextView(context);
hourView.setText(""+i+":00");
BrScreenMetrics.getScreenWidthPX()-20, lineHeight);
positionView(hourView, 50, i*lineHeight, BrScreenMetrics.getScreenWidthPX()-20, lineHeight);
dayView.addView(hourView);
}
dayViewContainer.addView(dayView);
addView(dayViewContainer);
I have a complex timetable to layout using TextViews
. I see that absolute layout has been depreciated. Now i know it is "bad practice" but i have a well formed screen metrics class that will adjust everything in relation to screen size/density etc.
If i cannot use absolute layout, then how can i tell the TextView
I want it so much left and top? (like in HTML using an absolute position)?
I want to do this programmatically not using XML. Is this possible or do i have to use a WebView
or canvas?
EDIT::
void positionView (TextView v, int x, int y, int w, int h){
final ViewGroup.MarginLayoutParams lpt = new ViewGroup.MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);//v.getLayoutParams();
//lpt.setMargins(x,y,x+w,y+h);
lpt.height=h;
lpt.width=w;
lpt.topMargin=y;
lpt.leftMargin=x;
v.setLayoutParams(lpt);
}
This is the method i am calling from this quick testcode:
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(0xFF00FF00)
int lineHeight = 60;
for(int i =0;i<11 ;i++){
TextView hourView = new TextView(context);
hourView.setText(""+i+":00");
BrScreenMetrics.getScreenWidthPX()-20, lineHeight);
positionView(hourView, 50, i*lineHeight, BrScreenMetrics.getScreenWidthPX()-20, lineHeight);
dayView.addView(hourView);
}
dayViewContainer.addView(dayView);
addView(dayViewContainer);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,经过整整 24 小时的绞尽脑汁,我终于为自己和其他人找到了答案:D
感谢大家,你们的意见无疑给我指明了正确的方向!
基本上,框架布局中的边距不能正常工作,除非我遗漏了一些东西。所以我所做的就是必须扩展一个完全扩展的线性布局,并在其中添加所需的视图边距。
例如:
}
然后可以将其添加到 FrameLayout 并将视图显示在正确的位置。
示例:
Ok after a whole 24 hours of headscratching i finally came up with an answer for myself and anyone else :D
Thanks to everyone, your input certainly put me in right direction!
Basically margins in framelayout DO NOT WORK properly unless i am missing something. So what i have done is have to extend a fully expanded linear layout with the required view margined inside it.
For example:
}
This can then be added to the FrameLayout and will display the view in the right position.
Examples:
是的,您可以使用以下代码以编程方式完成此操作。
Yes you can do it programmatically by using the following code..