【问题标题】:How do make layout in android studio?如何在 android studio 中进行布局?
【发布时间】:2021-03-27 02:27:35
【问题描述】:

我正在尝试进行如下所示的活动: here

问题是当我启动应用程序时它并没有得到相同的结果。按钮被剪切或文本被剪切。如果我改变一个按钮的位置,一切都在左上角。 我的问题是:我该怎么做才能让一切都得到我想要的位置? 我使用了相对布局。也许这就是问题所在?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button3"
        android:layout_width="245dp"
        android:layout_height="68dp"
        android:layout_below="@+id/button"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="83dp"
        android:layout_marginTop="26dp"
        android:layout_marginEnd="83dp"
        android:layout_marginBottom="238dp"
        android:text="Info"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        android:textStyle="normal" />

    <Button
        android:id="@+id/button"
        android:layout_width="245dp"
        android:layout_height="68dp"
        android:layout_centerInParent="true"
        android:text="Kartenansicht"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="115dp"
        android:layout_above="@+id/button2"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="71dp"
        android:layout_marginTop="112dp"
        android:layout_marginEnd="73dp"
        android:layout_marginBottom="18dp"
        android:text="Um zu überprüfen, ob Sicherheitskameras in der Nähe sind, bitte hier drücken:"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="244dp"
        android:layout_height="62dp"
        android:layout_above="@+id/button"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="83dp"
        android:layout_marginEnd="84dp"
        android:layout_marginBottom="24dp"
        android:text="Start"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp" />
</RelativeLayout>

【问题讨论】:

  • 1.为 Buttons 设置一个固定的 HEIGHT 会多次导致不良行为(以裁剪文本为主)。请改用 WRAP_CONTENT。 2.RelativeLayout 已“过时”并被更新的 ConstraintLayout 取代

标签: java android android-studio button layout


【解决方案1】:

你的布局一团糟。例如:

    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"

这两个参数不能共存。 对于您想要的布局,您应该使用 LinearLayout 并将 Orientation 设置为垂直。这样,您需要做的就是将您的 UI 对象 1 写在另一个之下,它们将被放置在另一个之上。为了使它们居中,您可以使用重力和 layout_gravity

            android:layout_gravity="center"
            android:gravity="center"

这应该就是全部了。

编辑: 只是为了让您知道,相对布局基本上将任何给定对象放置在布局的位置 0,0。如果您创建的对象很少,它们将被放置在另一个之上。

【讨论】:

    【解决方案2】:

    使用约束布局,例如,在您可以将宽度设置为 0dp 后,为每个视图设置一个开始和结束约束

    【讨论】:

    • 我转换成Constraint Layout 现在所有东西都放在左上角了
    • 好的,我明白了。我不得不添加约束。谢谢
    【解决方案3】:

    如果你想通过拖放来创建设计,那么最好使用约束布局而不是相对布局,但是如果你还是喜欢使用相对布局,你应该使用 "centerInParent = "true " "在中间按钮中,为上面的按钮设置 "layout_above = "@+id/your_centered_view_id""layout_below = "@+id/your_centered_view_id" "。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 2021-07-09
      相关资源
      最近更新 更多