在使用XML的约束布局中为TextView添加边距

11

我想给TextView小部件添加左右边距,但似乎没有生效。

这是我的活动XML:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sayhi.DisplayOutput"
    tools:layout_editor_absoluteY="81dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="140dp"
        android:text="@string/output"
        android:textSize="30sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

以下是它的外观:

在这里输入图片描述

我正在使用GUI布局编辑器。我尝试为小部件添加填充,填充按预期工作。我认为我错过了一些关于布局基础的东西,但我不确定是什么。

我正在使用Android Studio 2.3、ConstraintLayout 1.0.2、AppCompat v7:26、Build Tools Version 26.0.2。

2个回答

17

变更

android:layout_width="wrap_content"

android:layout_width="match_parent"
// or 
android:layout_width="0dp"

TextView的宽度。


1
两者都可以工作!+1 给 match_parent,因为它是自我描述的。抱歉回复晚了。我会接受这个答案。 - vadasambar

5
TextView的宽度设置为0dp
<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    ... />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接