zoukankan      html  css  js  c++  java
  • android实现布局重叠

    主要用到的类方法是view类下的layout,layout定义当前控件的左上角相对父节点的左上右下的距离。

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);
    final int width = dm.widthPixels;
    final int height = dm.heightPixels;

    final ImageView iv = (ImageView) this.findViewById(R.id.iv);
    iv.setOnClickListener(new OnClickListener() {
    Random random = new Random();
    public void onClick(View v) {
    int l = random.nextInt(width);
    int t = random.nextInt(height);

    int ivWidth = iv.getMeasuredWidth();
    int ivHeight = iv.getMeasuredHeight();

    if (l > width - ivWidth) {
    l = width - ivWidth;
    }

    if (t > height - ivHeight - 50) {
    t = height - ivHeight - 50;
    }

    Log.v("btn l,t:", "" + l + "," + t);
    iv.layout(l, t, l + ivWidth, t + ivHeight);
    }
    });

    资源文件:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="hello" />
    <ImageView
    android:id="@+id/iv"
    android:src="@drawable/ic_launcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    </RelativeLayout>

  • 相关阅读:
    c++中for的四种用法
    同步
    排列算法(reverse...rotate...next_permutation)
    指针
    const的使用
    const_cast
    c++设计模式——单例模式
    c++中的流
    c++从string类型转换为bool类型
    结构体字节对齐方式
  • 原文地址:https://www.cnblogs.com/canphp/p/2731004.html
Copyright © 2011-2022 走看看