【问题标题】:What is the right way to add margin to this views?为该视图添加边距的正确方法是什么?
【发布时间】:2021-08-02 08:48:31
【问题描述】:

我有问题。 我想为一组按钮和文本视图添加背景。问题是我希望背景比输入有一个余量,所以它看起来还不错。
我无法在容器布局中创建视图,然后为他添加背景,因为我需要与所有其他视图具有相同布局的视图。所以,我做了一个带有背景的视图并将其限制在所需的视图中。
如何在视图的背景上添加边距?无需手动为每个视图添加边距。 除了创建带背景的视图之外,也许还有其他选择?

它的样子

我想要它

提前致谢

编辑: 忘记添加代码了。这里是:\

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp">

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/edit_add_card_group_shape"
    app:layout_constraintStart_toStartOf="@id/tvContact"
    app:layout_constraintEnd_toEndOf="@id/etPhoneNumber"
    app:layout_constraintTop_toTopOf="@id/btAreaCode"
    app:layout_constraintBottom_toBottomOf="@id/btAddPhone" />

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/etPhoneNumber"
    style="@style/Widget.App.EditText.TextSize"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:autofillHints=""
    android:hint="@string/phone_number"
    android:inputType="phone"
    android:layout_marginEnd="10dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="@+id/guideline3"
    app:layout_constraintTop_toBottomOf="@id/tvContact" />

<com.google.android.material.button.MaterialButton
    android:id="@+id/btAreaCode"
    style="@style/Widget.App.Button.TextButton.RegularColor"
    android:layout_width="0dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"
    android:layout_height="wrap_content"
    android:drawableEnd="@drawable/ic_baseline_arrow_forward_ios_24"
    android:text="@string/phone"
    android:textAlignment="textStart"
    app:layout_constraintBottom_toBottomOf="@id/etPhoneNumber"
    app:layout_constraintEnd_toStartOf="@+id/etPhoneNumber"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/tvContact"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@id/etPhoneNumber" />

<com.google.android.material.button.MaterialButton
    android:id="@+id/btAddPhone"
    style="@style/Widget.App.Button.TextButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/add_phone_number"
    android:textAlignment="textStart"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginBottom="10dp"
    android:layout_marginStart="10dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/etPhoneNumber2" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 分享你的代码
  • 嘿,我已经编辑了帖子

标签: android android-layout android-xml


【解决方案1】:

在你的 ConstraintLayout 中使用 android:padding

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:padding="12dp">

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 感谢您的回答!我会试试这个。但它不会和现在一样吗?我的意思是,我的视图仍然不会在按钮和编辑文本上留出边距
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
相关资源
最近更新 更多