onCreate中使用view.setX(380); view.setY(380);设置控件的位置,布局完成之后打印结果为:
onCreate中使用 layoutParams.topMargin = 380; layoutParams.leftMargin = 380;设置控件的位置,布局完成之后打印结果为:
测试代码如下
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
FrameLayout flContainer = findViewById(R.id.fl_container);
ImageView imageView = findViewById(R.id.image_view);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// layoutParams.topMargin = 380;
// layoutParams.leftMargin = 380;
imageView.setLayoutParams(layoutParams);
imageView.setX(380);
imageView.setY(380);
flContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FrameLayout.LayoutParams layoutParams1 = (FrameLayout.LayoutParams) imageView.getLayoutParams();
Log.e("xxxxxxx", "leftMargin:" + layoutParams1.leftMargin + "---topMargin:" + layoutParams1.topMargin);
Log.e("xxxxxxx", "getX:" + imageView.getX() + "---getY:" + imageView.getY());
public static void actionStart(Context context) {
Intent intent = new Intent(context, SecondActivity.class);
context.startActivity(intent);
结论:在设置控件位置时,推荐使用 layoutParams.topMargin,layoutParams.leftMargin,能避免一些布局及动画相关的坑。
onCreate中使用view.setX(380); view.setY(380);设置控件的位置,布局完成之后打印结果为:onCreate中使用layoutParams.topMargin = 380; layoutParams.leftMargin = 380;设置控件的位置,布局完成之后打印结果为:测试代码如下public class SecondActivity extends AppCompatActivity { @Override prote...