【问题标题】:ImageVIew in wrong positionImageView 在错误的位置
【发布时间】:2018-07-22 18:26:06
【问题描述】:

当我在activity_main.xml 的设计选项卡上的屏幕上放置ImageView 并运行程序时,ImageView 总是转到屏幕的左上角,而不是我放置它的位置。 如果我尝试使用如下代码放置它:

    LinearLayout meulayout = new LinearLayout(this);
    meulayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));


ImageView meuview = new ImageView(this);
meuview.setImageResource(R.drawable.vermelho);
meuview.setX(1000f);
meuview.setY(1000f);

meulayout.addView(meuview);

它根本不会出现在屏幕上。

【问题讨论】:

    标签: java android-studio imageview location positioning


    【解决方案1】:

    最简单的方法是使用如下代码制作布局文件:

    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.vestasoft.vadim.xxx.MainActivity">
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>
    
    </RelativeLayout>
    

    您可以在 ImageView 组件中添加其他标签,以根据需要定位它。像您一样使用像素位置来硬定位组件是一种不好的方法。

    【讨论】:

    • 我也认为你的方式更好,但让我更困惑的是,我在代码中显示的方式曾经很弱,但突然停止工作。
    • 也许您在代码中添加了一些更改。还有我给你的建议。每次,当你开始你的代码时,做'build -> clean project',它会帮助你摆脱任何问题。
    猜你喜欢
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多