如何设置图像的大小和位置? [英] How to set the size and position of an image?

查看:133
本文介绍了如何设置图像的大小和位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在屏幕上设置图像(image2)的大小和位置,以使其相对于背景图像(image1)处于相同的位置和大小,无论屏幕大小如何。为此,我尝试在 Activity 类的 onCreate 方法中更改image2的布局参数:

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?

logcat中的错误是:

The error in the logcat is:

E / AndroidRuntime (296):java.lang.RuntimeException:无法启动活动Co
mponentInfo {com.soft.tobi / com.soft.tobi.Game_main}:java.lang.NullPointerExc
eption

E/AndroidRuntime( 296): java.lang.RuntimeException: Unable to start activity Co mponentInfo{com.soft.tobi/com.soft.tobi.Game_main}: java.lang.NullPointerExc eption

但是我不明白为什么任何东西都为null,因为所有id都存在于我的xml文件中:

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>

谢谢

推荐答案

我认为问题是图像视图的大小

I think problem is size of imageview

 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((image1.getWidth())/4, (image2.getHeight())/4);

也许它返回0,所以会出现此问题。尝试它

maybe it return 0 so this problem is occur..try it

这篇关于如何设置图像的大小和位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆