如何使用java更改图像的坐标
目标: 单击按钮时,我想将图片的X和Y坐标更改为 android:layout_x 35dp android:layout_y:541dp使用java代码而不是XML代码 问题: 我找不到正确的语法代码 你知道怎么做吗 谢谢大家! 信息: *我是android的新手 好几年没看到绝对布局了 在Android中,视图的位置实际上不是由视图本身或包含该视图的视图组指定的。相反,它由视图附带的LayoutParams指定 在xml文件中,创建视图时,宽度、高度、边距以及x和y值都位于LayoutParams对象内,该对象将自动创建并设置到视图中 LayoutParams是告诉容器如何定位视图的组件 因此,如果要更新ImageView的x和y,需要从其LayoutParams进行编辑 所以我在这里做的是获取通过xml创建的LayoutParams,并更改其中的x和y值。之后,我将修改后的LayoutParams设置回ImageView如何使用java更改图像的坐标,java,android,Java,Android,目标: 单击按钮时,我想将图片的X和Y坐标更改为 android:layout_x 35dp android:layout_y:541dp使用java代码而不是XML代码 问题: 我找不到正确的语法代码 你知道怎么做吗 谢谢大家! 信息: *我是android的新手 好几年没看到绝对布局了 在Android中,视图的位置实际上不是由视图本身或包含该视图的视图组指定的。相反,它由视图附带的LayoutParams指定 在xml文件中,创建视图时,宽度、高度、边距以及x和y值都位于LayoutPar
我相信setLayoutParams已经足以使父容器更新视图的位置,但万一它不。。。请求布局应该是 可能的副本谢谢你的帮助!
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jfdimarzio.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="158dp"
android:layout_y="77dp"
android:src="@drawable/cat" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="testtest"
android:text="Button" />
</AbsoluteLayout>
package com.jfdimarzio.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView myImageView = (ImageView) findViewById(R.id.imageView2);
myImageView .setImageResource(R.drawable.cat);
}
public void testtest(View view)
{
AbsoluteLayout relativeLayout = new AbsoluteLayout(this);
ImageView myImageView = (ImageView) findViewById(R.id.imageView2);
myImageView.layout(10,10,10,10);
//myImageView.layout(3,3,3,3);
}
}
ImageView myImageView = (ImageView) findViewById(R.id.imageView2);
AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) myImageView.getLayoutParams();
params.x = NEW_X_VALUE; //Please enter this yourself
params.y = NEW_Y_VALUE; //Be careful of DP to PX conversions
myImageView.setLayoutParams(params);
myImageView.requestLayout();