【问题标题】:Could youhelp me out in android xml? [duplicate]你能帮我解决android xml吗? [复制]
【发布时间】:2017-04-09 02:44:07
【问题描述】:

 <Button
        android:id="@+id/bintent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity"
        tools:layout_editor_absoluteY="132dp"
        tools:layout_editor_absoluteX="136dp" />

    <Button
        android:id="@+id/balram1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Alarm"
        android:layout_centerInParent="true"
        tools:layout_editor_absoluteY="231dp"
        tools:layout_editor_absoluteX="136dp" />

`

那么我应该怎么做才能在实际设备上显示它

【问题讨论】:

  • 你能粘贴完整的xml吗?
  • 此属性仅用于编辑器。 工具:属性已从代码中剥离,仅用于开发目的。正如我在上面看到的,它将绝对显示Button的位置

标签: android xml


【解决方案1】:

如果没有完整的 xml 文件,几乎不可能知道发生了什么。我假设父布局是 RelativeLayout,因为您使用的是 centerInParent 属性,但我不确定您为什么使用绝对 X 和 Y 值。他们甚至不会做任何事情,因为“工具”命名空间仅适用于布局编辑器。它根本不影响应用程序。

我最好的猜测是您将父布局的宽度和高度设置为“wrap_content”而不是“match_parent”,否则您的“警报”按钮将正确地在屏幕上居中。

【讨论】:

    【解决方案2】:

    您在这里遇到的问题是您使用了tools:layout_editor_absoluteXtools:layout_editor_absoluteY 属性。以tools 开头的属性仅供布局设计者使用。

    如果你想使按钮居中,你应该做的是使用相对布局作为父元素,在其中你可以放置你的按钮。所以应该是这样的:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_below="@+id/button2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="36dp"/>
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_alignParentTop="true"
            android:layout_alignStart="@+id/button3"
            android:layout_marginTop="42dp"/>
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      像所有其他答案一样,我也不确定您使用的是哪个父布局,但在我脑海中的某个地方是约束布局。如果是,请使用以下代码:)

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <Button
              android:id="@+id/bintent"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Activity"
              android:layout_marginLeft="8dp"
              app:layout_constraintLeft_toLeftOf="parent"
              android:layout_marginRight="8dp"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              android:layout_marginTop="150dp" />
      
          <Button
              android:id="@+id/balram1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Alarm"
              android:layout_centerInParent="true"
              android:layout_marginLeft="8dp"
              app:layout_constraintLeft_toLeftOf="parent"
              android:layout_marginRight="8dp"
              app:layout_constraintRight_toRightOf="parent"
              android:layout_marginTop="61dp"
              app:layout_constraintTop_toBottomOf="@+id/bintent" />
      </android.support.constraint.ConstraintLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-29
        • 1970-01-01
        • 1970-01-01
        • 2022-12-15
        • 2017-12-23
        • 2011-01-24
        相关资源
        最近更新 更多