无法在运行时将ImageView置于顶角

时间:2015-07-03 03:48:06

标签: android android-layout

我想在运行时在左上角放置一个ImageView,但它不起作用。 这是我的布局和代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
    android:id="@+id/layout_main"
    android:layout_height="fill_parent" tools:context=".Main">
</RelativeLayout>

这是代码:

RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout_main);
ImageView imageView =new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.green_apple);
imageView.setScaleX(0.3f);
imageView.setScaleY(0.3f);
imageView.setLeft(0);
imageView.setTop(0);
layout.addView(imageView);

我也尝试过:

RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout_main);
ImageView imageView =new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.green_apple);
imageView.setScaleX(0.3f);
imageView.setScaleY(0.3f);
imageView.setX(0);
imageView.setY(0);
layout.addView(imageView);

RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout_main);
ImageView imageView =new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.green_apple);
imageView.setScaleX(0.3f);
imageView.setScaleY(0.3f);
layout.addView(imageView);
set = new AnimatorSet();        
ObjectAnimator aniX = ObjectAnimator.ofFloat(imageView, "x", 0);
aniX.setDuration(100);
ObjectAnimator aniY = ObjectAnimator.ofFloat(imageView, "y", 0);
aniY.setDuration(100);
set.playTogether(aniX, aniY);
set.start()

但同样的结果,这是我的结果:enter image description here

总是有大空间到顶部和左侧屏幕,虽然我设置0,0。我也尝试设置屏幕的高度和宽度,它飞出屏幕。我不知道为什么。 非常感谢,任何人都可以解释,以及如何帮助我解决它

1 个答案:

答案 0 :(得分:1)

那是因为你使用了setScaleX和setScaleY,所以请尝试下面的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<ul>
  <li>a
  </li>
  <li>b
  </li>
  <li>c
  </li>
  <li>d
  </li>
  <li>e
  </li>
  <li>f
  </li>
  <li>g
  </li>
</ul>