Java 如何相对于设备屏幕大小放置图像按钮
我有一张图片,上面已经有按钮的所有文本,我想在每个单词的顶部放一个透明的按钮。 我不能使用XML代码,因为如果我想让按钮停留在文本上,代码必须更改 以下是XML代码:Java 如何相对于设备屏幕大小放置图像按钮,java,android,xml,Java,Android,Xml,我有一张图片,上面已经有按钮的所有文本,我想在每个单词的顶部放一个透明的按钮。 我不能使用XML代码,因为如果我想让按钮停留在文本上,代码必须更改 以下是XML代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/backgroundMenue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="Nothing"
android:scaleType="centerCrop"
android:src="@drawable/main_menu2" />
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="92dp"
android:layout_marginRight="218dp" />
</RelativeLayout>
Brother您可以使用displaymetrics类并使用getheight和getwidth方法,该方法返回您的屏幕高度和宽度,然后根据设置图像高度和宽度。我不想为每个设备重新调整按钮或图像的大小。我想把我的按钮放在背景图片上的文本框上。如果你知道文本在屏幕上的位置,你可以使用LayoutParams以编程方式更改按钮的位置position@Rani你能解释一下你是怎么做的吗,因为我找不到方法。
package com.example.snakesmash;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
ImageButton play;
ImageView background;
RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = (RelativeLayout) RelativeLayout.inflate(this, R.layout.activity_main, null);
play = (ImageButton) layout.findViewById(R.id.play);
background = (ImageView) layout.findViewById(R.id.backgroundMenue);
play.layout((int) Math.round(background.getHeight() * 0.65), (int) Math.round(background.getWidth() * 0.65), (int) Math.round(background.getWidth() * 0.35), (int) Math.round(background.getHeight() * 0.35));
setContentView(layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}