【问题标题】:Image position resets to zero at runtime in Android Studio图像位置在 Android Studio 运行时重置为零
【发布时间】:2017-04-25 20:57:08
【问题描述】:

我在预览中设置了一些图像按钮,在 xml 文件中设置了它们的位置,但是当我运行程序时,所有图像都会移动到 (0,0)。

<ImageButton
    android:id="@+id/imageButton3"
    android:layout_width="126dp"
    android:layout_height="117dp"
    android:background="@null"
    app:srcCompat="@drawable/sad"
    tools:layout_editor_absoluteY="82dp"
    tools:layout_editor_absoluteX="200dp" />

IDE 提示我:

布局编辑器允许您将小部件放置在画布上的任何位置,并使用设计时属性(例如 layout_editor_absoluteX)记录当前位置。这些属性不会在运行时应用,因此如果您将布局推送到设备上,小部件可能出现在与编辑器中显示不同的位置。要解决此问题,请通过从边缘连接拖动来确保小部件同时具有水平和垂直约束。

如何在运行时设置图像的值?

【问题讨论】:

    标签: java android imagebutton


    【解决方案1】:

    您可以使用 FrameLayout 或 RelativeLayout 并设置左边距和上边距来放置您的视图。

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="126dp"
            android:layout_height="117dp"
            android:background="@null"
            app:srcCompat="@drawable/sad"
            android:layout_marginLeft="82dp"
            android:layout_marginTop="200dp" />
    
    </FrameLayout>
    

    【讨论】:

    • 谢谢,正是我要找的东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多