Android 什么是0,0声明,在";。设置边距(imgX,imgY,0,0)“?我能正确理解X,Y坐标吗?
我目前正在尝试将ImageButton移动到Android应用程序中的某个X和Y位置。(Api级别15到22)并且图像的大小不保持不变。这是扭曲的。我首先假设是在以下代码中声明0,0:Android 什么是0,0声明,在";。设置边距(imgX,imgY,0,0)“?我能正确理解X,Y坐标吗?,android,imagebutton,Android,Imagebutton,我目前正在尝试将ImageButton移动到Android应用程序中的某个X和Y位置。(Api级别15到22)并且图像的大小不保持不变。这是扭曲的。我首先假设是在以下代码中声明0,0: marginParams.setMargins(imgX, imgY, 0, 0); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams); img.setLayoutParams(layo
marginParams.setMargins(imgX, imgY, 0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
img.setLayoutParams(layoutParams);
所以我试着包装内容。但它什么也没做
marginParams.setMargins(imgX, imgY, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
img.setLayoutParams(layoutParams);
以下是所有代码(如果需要):
public class MainActivity extends ActionBarActivity {
public EditText collectedTextEdit;
public ImageButton candyEdit;
public int collected = 0;
public int screenWidth = 300;
public int screenHeight = 300;
public final int candySize = 50;
Random random = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
collectedTextEdit = (EditText) findViewById(R.id.collectedText);
candyEdit = (ImageButton) findViewById(R.id.candy);
collectedTextEdit.setText("Collected: " + collected);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
addListenerOnButton();
}
public void addListenerOnButton() {
candyEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
collected += 1;
collectedTextEdit.setText("Collected: " + collected);
int candyX = random.nextInt(screenWidth);
int candyY = random.nextInt(screenHeight);
System.out.println(candyX + " : " + candyY);
//RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
MarginLayoutParams marginParams = new MarginLayoutParams(candyEdit.getLayoutParams());
marginParams.setMargins(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, candyX, candyY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
//layoutParams.WRAP_CONTENT;
candyEdit.setLayoutParams(layoutParams);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
下面是ImageButton的声明:
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/candy"
android:background="@drawable/candy"
android:contentDescription="@string/candyImg"
android:clickable="true"
android:layout_below="@+id/collectedText"
android:layout_alignRight="@+id/collectedText"
android:layout_alignEnd="@+id/collectedText" />
请帮帮我!如果您需要任何其他代码示例,请随时询问,我会马上开始 你能用imagebutton显示你的代码是什么样子吗?当然,现在就添加它!:)出于好奇,您是否尝试过在xml中使高度/宽度保持静态?e、 g.30dp是的,我会在申报时出示