【问题标题】:RelativeLayout different position of items in editor and on deviceRelativeLayout 项目在编辑器和设备上的不同位置
【发布时间】:2018-07-21 06:24:18
【问题描述】:

我正在尝试使用 3 个项目在 RelativeLayout 中制作页脚 - 一个 ImageView、一个 TextView 和一个 Button。我设法将ImageView 定位在布局的左侧,将TextView 定位在左侧的ImageView 附近。但是,我无法在布局右侧正确设置按钮的位置。编辑器和我的设备中的位置不同(当我测试它时)。当我在编辑器中以我想要的方式放置所有内容时,在设备上运行它后它看起来不一样。应该在布局右侧的Button 离开屏幕。

例如this 是我在编辑器中看到的,this 是我在设备上看到的。如果我在编辑器中将按钮移动到它应该在的位置like this,然后它会在设备上离开屏幕like this

我也尝试使用LinearLayoutgravitylayout_gravityweight 以及填充和边距的不同组合,但我就是无法让它工作,我不明白问题出在哪里。

这是我的 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 android-layout android-button android-relativelayout


    【解决方案1】:

    只需在右键中使用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"/>
    

    【讨论】:

      【解决方案2】:

      您的完整布局应如下所示。您需要正确使用layout_alignParentLeftlayout_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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 2018-05-09
        • 2010-11-23
        • 2015-12-30
        相关资源
        最近更新 更多