RelativeLayout 项目在编辑器和设备上的不同位置
RelativeLayout different position of items in editor and on device
我正在尝试使用 3 项在 RelativeLayout
中制作页脚 - 一项 ImageView
、一项 TextView
和一项 Button
。我设法将 ImageView
放置在布局的左侧,将 TextView
放置在左侧的 ImageView
附近。但是,我无法在布局右侧正确设置按钮的位置。编辑器和我的设备中的位置不同(当我测试它时)。当我在编辑器中按照我想要的方式放置所有内容时,在 运行 之后它在设备上看起来不一样了。 Button
应该在布局右侧的那个会离开屏幕。
例如this is what I see in editor, and this is what I see on device. If I move button in editor, on position where it should be, like this, then it goes off screen on device, like this.
我也尝试过使用 LinearLayout
、gravity
、layout_gravity
、weight
以及填充和边距的不同组合,但我就是无法让它工作,我不明白问题出在哪里。
这是我的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background" />
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:src="@mipmap/help" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="60dp"
android:paddingTop="10dp"
android:text="help"
android:textColor="@color/mainWhite" />
<Button
android:id="@+id/helpButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginLeft="330dp"
android:layout_marginTop="3dp"
android:background="@mipmap/Continue" />
</RelativeLayout>
谢谢。
只需使用右键android:layout_alignParentEnd="true"
<Button
android:id="@+id/helpButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginTop="3dp"
android:layout_alignParentEnd="true"
android:background="@android:drawable/ic_input_add"/>
您的完整布局应如下所示。您需要正确使用 layout_alignParentLeft
和 layout_alignParentRight
。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@drawable/background"
android:padding="4dp">
<ImageView
android:id="@+id/question_icon"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp"
android:padding="3dp"
android:src="@mipmap/help" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/question_icon"
android:text="Help"
android:textColor="@android:color/white" />
<Button
android:id="@+id/helpButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:background="@mipmap/Continue" />
</RelativeLayout>
我正在尝试使用 3 项在 RelativeLayout
中制作页脚 - 一项 ImageView
、一项 TextView
和一项 Button
。我设法将 ImageView
放置在布局的左侧,将 TextView
放置在左侧的 ImageView
附近。但是,我无法在布局右侧正确设置按钮的位置。编辑器和我的设备中的位置不同(当我测试它时)。当我在编辑器中按照我想要的方式放置所有内容时,在 运行 之后它在设备上看起来不一样了。 Button
应该在布局右侧的那个会离开屏幕。
例如this is what I see in editor, and this is what I see on device. If I move button in editor, on position where it should be, like this, then it goes off screen on device, like this.
我也尝试过使用 LinearLayout
、gravity
、layout_gravity
、weight
以及填充和边距的不同组合,但我就是无法让它工作,我不明白问题出在哪里。
这是我的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background" />
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:src="@mipmap/help" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="60dp"
android:paddingTop="10dp"
android:text="help"
android:textColor="@color/mainWhite" />
<Button
android:id="@+id/helpButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginLeft="330dp"
android:layout_marginTop="3dp"
android:background="@mipmap/Continue" />
</RelativeLayout>
谢谢。
只需使用右键android:layout_alignParentEnd="true"
<Button
android:id="@+id/helpButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginTop="3dp"
android:layout_alignParentEnd="true"
android:background="@android:drawable/ic_input_add"/>
您的完整布局应如下所示。您需要正确使用 layout_alignParentLeft
和 layout_alignParentRight
。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@drawable/background"
android:padding="4dp">
<ImageView
android:id="@+id/question_icon"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp"
android:padding="3dp"
android:src="@mipmap/help" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/question_icon"
android:text="Help"
android:textColor="@android:color/white" />
<Button
android:id="@+id/helpButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:background="@mipmap/Continue" />
</RelativeLayout>