请问怎么动态设置一个控件的绝对位置?

SUMI_SUMI 2011-07-25 01:20:53
我看了下CSDN博客中的文章,然后代码如下:
在OnGreat中对某一个TextView操作
final TextView widget79 = (TextView) findViewById(R.id.widget79);
AbsoluteLayout.LayoutParams linearParams = (AbsoluteLayout.LayoutParams)widget79.getLayoutParam();
linearParams.height = 70;
widget79.setLayoutParams(linearParams);

这样程序运行会报错关闭,屏蔽以上代码就正常
另外,怎么设置绝对位置?
...全文
1234 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
nliuser 2011-07-28
  • 打赏
  • 举报
回复
好像只有绝对布局才行。
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(w, h, x, y);
mLayout.updateViewLayout(mLayoutScrollView, params);
SUMI_SUMI 2011-07-28
  • 打赏
  • 举报
回复
即是说不能在xml中先添加一个控件,比如textview
然后再在OnGreat中设置其属性,大小位置等?
fishmen26 2011-07-26
  • 打赏
  • 举报
回复
[Quote=引用楼主 sumi_sumi 的回复:]
我看了下CSDN博客中的文章,然后代码如下:
在OnGreat中对某一个TextView操作
final TextView widget79 = (TextView) findViewById(R.id.widget79);
AbsoluteLayout.LayoutParams linearParams = (AbsoluteLayout.LayoutParams)……
[/Quote]

final TextView widget79 = (TextView) findViewById(R.id.widget79) ,这种写在xml里面的 View 是不能控制其在screen上的位置吧。
只能new 一个新的view ,然后指定它的位置,再用layout的 addView 方法把它添加到layout中。
ch_984326013 2011-07-25
  • 打赏
  • 举报
回复
http://blog.csdn.net/ch_984326013/article/details/6618680可以去看下这篇文章。动态设置表格,也就是动态设置TextView控件!
fishmen26 2011-07-25
  • 打赏
  • 举报
回复
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
Button mButtonLeft = (Button)findViewById(R.id.button01);
Button mButtonRight = (Button)findViewById(R.id.button02);
mButtonLeft.setWidth(screenWidth / 3);
mButtonRight.setWidth((screenWidth * 2) / 3);

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧