【问题标题】:I can't change position of textview and buttons in layout editor我无法在布局编辑器中更改 textview 和按钮的位置
【发布时间】:2019-03-01 12:32:26
【问题描述】:

我的布局预览不只显示我添加的文本视图和按钮的操作栏,当我添加一个新按钮或文本视图时,即使我尝试使用相对和约束布局,它也只是转到屏幕的左上角.我试过使用"Base.Theme.AppCompat.Light.DarkActionBar",但没有解决。自从我在我的新 PC 上安装了 android studio 后,我就遇到了这个问题。Here's a screenshot of my layout preview

<?xml version="1.0" encoding="utf-8"?>
<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">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Create Account" />
</RelativeLayout>

【问题讨论】:

标签: android


【解决方案1】:

如果你不设置它的位置,你的按钮在左上角是完全正常的。

如果您将此添加到您的按钮:

android:layout_centerInParent="true"

您的 Button 将位于根布局的中心。

【讨论】:

  • 谢谢它的工作。只是在我的旧电脑上,我可以把它拖到我想要的任何地方。
  • 拖放解决方案的问题是您将使用绝对位置设置 UI 元素,但是由于您需要使用相对位置来处理许多不同的屏幕尺寸,因此您的 UI 在每个位置上都是相同的设备。
  • 好的。感谢您的帮助,非常感谢。
【解决方案2】:

您甚至可以使用 ConstraintLayout 来解决这个问题。借助约束布局句柄,您可以轻松地将按钮设置为中心位置。constraint layout centering button

很容易做到:先加入右把手,再加入左把手,再加入上下把手。

同样借助这个,布局在不同的屏幕上也不会失真

【讨论】:

    猜你喜欢
    • 2016-05-05
    • 2021-04-12
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    相关资源
    最近更新 更多