Android--取得控件在手机屏幕上的位置
2013-02-16 16:15
295 查看
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; /** * 如果在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); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/button2" android:layout_width="80dp" android:layout_height="80dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="150dp" android:onClick="click" android:text="Button" /> </RelativeLayout>
相关文章推荐
- Android获取控件在手机屏幕上的位置
- android 把控件放在屏幕任意位置
- Android中取得手机屏幕的信息
- android获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- 3.5 MonoForAndroid用户人机界面-取得手机屏幕大小
- Android获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- Android SDK开发范例大全 ---3.5取得手机屏幕的大小
- android得到控件在屏幕中的位置
- Android中利用DisplayMetrics取得手机屏幕大小
- android获取屏幕控件位置问题
- Android基础教程(四)之-----取得手机屏幕大小DisplayMetrics的应用
- android获取自己定义控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- Android获取手机屏幕大小 — DisplayMetrics取得画面宽高的方法
- Android 屏幕适配攻略(四)获取手机屏幕的相关信息 与动态设置控件的大小
- 【Android Demo】Android中取得手机屏幕大小
- android获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- android中取得手机屏幕大小
- Android中利用DisplayMetrics取得手机屏幕大小
- Android 获取控件相对于屏幕位置
- 【Android】控件与屏幕的位置、坐标、大小