Android的 - 使用view.setX()和塞蒂在API 8 [英] Android - Use of view.setX() and setY in api 8

查看:848
本文介绍了Android的 - 使用view.setX()和塞蒂在API 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的机器人api8。 我希望定位/动态放置在屏幕上的景色。 但使用setX的和SETY我们需要API级别11以上。 我们如何使用它的API 8或是否有任何其他的吗? 需要帮助

i am working on android api8. i want to position/place a view on screen dynamically. but to use setX and setY we need API level 11 and above. how can we use it in API 8 or is there any alternative for this? need help

推荐答案

您可以使用的LayoutParams 做到这一点。 这些可以被添加到所述机器人接口的组件来设定它们的范围和位置。

You can do this by using the LayoutParams. These can be added to components of the android interface to set their bounds and position.

这是例子(设置在RelativeLayout的一个子视图的LayoutParams)

An example (setting the LayoutParams on a child view of a RelativeLayout)

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //The WRAP_CONTENT parameters can be replaced by an absolute width and height or the FILL_PARENT option)
params.leftMargin = 50; //Your X coordinate
params.topMargin = 60; //Your Y coordinate
childView.setLayoutParams(params);

在做这个的LayoutParams的类型必须为要增加他们的childview的父。 ( LinearLayout.LayoutParams 的LinearLayout RelativeLayout.LayoutParams RelativeLayout的等)。

而不是 childView.setLayoutParams(PARAMS)另外,; 你也可以使用 parentView.addView(childView,则params); 设定的LayoutParams时,该项目将被添加到父容器。

Also, instead of childView.setLayoutParams(params); you can also use parentView.addView(childView,params); to set the Layoutparams when the item is added to the parent container.

注意!作为坐标的值以像素为单位。由于它是用DP值来定义你的界面大小的最佳实践,您可以使用这片code到DP像素转换:

NOTE! The values for the coordinates are in pixels. Since it's best practice to use DP values to define your interface sizes you could use this piece of code to convert dp to pixels:

private int getPixels(int dipValue){ 
     Resources r = getResources();
     int px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue,   r.getDisplayMetrics());
     return px; 
}

这篇关于Android的 - 使用view.setX()和塞蒂在API 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆