如何设置图片的大小和位置?
我正在尝试设置屏幕上图像(image2)的大小和位置,以便无论屏幕大小如何,它相对于背景图像(image1)都处于相同的位置和大小。为此,我尝试在 Activity
类的 onCreate
方法中更改 image2 的布局参数:
public class Game_main extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ImageView image2= (ImageView) findViewById(R.id.seed1);
ImageView image1= (ImageView) findViewById(R.id.board_rev1);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rlGame);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((image1.getWidth())/4, (image2.getHeight())/4);
params.leftMargin = (image1.getWidth())/2;
params.topMargin = (image1.getHeight())/4;
rl.addView(image2, params);
setContentView(R.layout.gamerev);
}
}
但是当我的应用程序转到此 Activity 时,我收到错误:说应用程序意外停止。然后我必须强制关闭该应用程序。为什么会出现这种情况呢?我有什么遗漏的吗?或者有更好的方法来做到这一点吗?
logcat中的错误是:
E/AndroidRuntime(296): java.lang.RuntimeException: 无法启动活动Co mponentInfo{com.soft.tobi/com.soft.tobi.Game_main}: java.lang.NullPointerExc 但我不明白
为什么任何内容都会为空,因为所有 id 都存在于我的 xml 文件中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlGame"
>
<ImageView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/board_rev1" android:src="@drawable/board_rev1"></ImageView>
<ImageView android:id="@+id/seed1" android:src="@drawable/seed1" android:adjustViewBounds="true" android:layout_height="200dp" android:layout_marginTop="165dp" android:layout_width="200dp" android:layout_marginLeft="30dp"></ImageView>
</RelativeLayout>
谢谢
I am trying to set the size and position of an image (image2) on the screen so that it is in the same position and size relative to the background image (image1) it is on no matter the screen size. To do this, I try to alter the layout parameters of image2 in my onCreate
method of my Activity
class:
public class Game_main extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ImageView image2= (ImageView) findViewById(R.id.seed1);
ImageView image1= (ImageView) findViewById(R.id.board_rev1);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rlGame);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((image1.getWidth())/4, (image2.getHeight())/4);
params.leftMargin = (image1.getWidth())/2;
params.topMargin = (image1.getHeight())/4;
rl.addView(image2, params);
setContentView(R.layout.gamerev);
}
}
But when my app goes to this activity, I get an error that says the application has stopped unexpectedly. Then I have to force close the app. Why would this happen? Is there something I am missing? or is there a better way to do this?
The error in the logcat is:
E/AndroidRuntime( 296): java.lang.RuntimeException: Unable to start activity Co
mponentInfo{com.soft.tobi/com.soft.tobi.Game_main}: java.lang.NullPointerExc
eption
But I dont see why anything would be null because all the ids exist in my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlGame"
>
<ImageView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/board_rev1" android:src="@drawable/board_rev1"></ImageView>
<ImageView android:id="@+id/seed1" android:src="@drawable/seed1" android:adjustViewBounds="true" android:layout_height="200dp" android:layout_marginTop="165dp" android:layout_width="200dp" android:layout_marginLeft="30dp"></ImageView>
</RelativeLayout>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否放置了 try catch 块并尝试调试它?似乎有什么东西返回null..
Did you put a try catch block and try to debug it ? It seems to be something returns null..
我认为问题是 imageview 的大小
,也许它返回 0 所以这个问题发生了..试试吧
I think problem is size of imageview
maybe it return 0 so this problem is occur..try it