设为首页 加入收藏

TOP

Android开发教程:取得控件在手机屏幕上的位置
2014-11-24 08:22:48 来源: 作者: 【 】 浏览:1
Tags:Android 开发 教程 取得 控件 手机 屏幕 位置

/**
* 如果在Activity的OnCreate()事件输出控件位置数据,是全为0,要等UI控件都加载完了才能获取到数据。
* @author ZLQ
*
*/
public class GetLocationData extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

/*
* 单击按钮事件
*/
public void click(View v){
View view = findViewById(R.id.button2);
//数组长度必须为2
int[] locations = new int[2];
view.getLocationOnScreen(locations);
int x = locations[0];//获取组件当前位置的横坐标
int y = locations[1];//获取组件当前位置的纵坐标
Log.i("System.out", "x:" + x + "y:" + y);
}
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android基础教程:自定义带提示文.. 下一篇Linux2.6移植:DM9000驱动

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python 数据分析与可 (2025-12-26 21:51:20)
·从零开始学Python之 (2025-12-26 21:51:17)
·超长干货:Python实 (2025-12-26 21:51:14)
·为什么 Java 社区至 (2025-12-26 21:19:10)
·Java多线程阻塞队列 (2025-12-26 21:19:07)