Reputation: 351
Why I can not set a position for an ImageView
on the layout with programming?
when I run the following code when I click on the ImageView
object it should go to another place but it stays in the same place. Note I know there are methods like the setX()
or setY()
, but I am using the API 7.
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/imageView"
android:src="@drawable/aaaaaaaaaaaaaa"
android:layout_centerInParent="true"
/>
And
public class MainActivity extends Activity {
ImageView imageView;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
params.leftMargin = 30;
imageView.requestLayout();
}
});
}
}
Upvotes: 1
Views: 55
Reputation: 91
i bet you will have to set layout params after change:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
params.leftMargin = 30;
imageView.setLayoutParams(params);
Upvotes: 2