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

vad*_*bar 4 android android-layout android-constraintlayout

我想向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>
Run Code Online (Sandbox Code Playgroud)

看起来是这样的:

在此处输入图片说明

我正在使用GUI布局编辑器。我尝试将填充添加到小部件中,并且填充按预期工作。我想我缺少有关布局的一些基本知识,但我不确定是什么。

我正在使用Android Studio 2.3,ConstraintLayout 1.0.2,AppCompat v7:26,构建工具版本26.0.2。

KeL*_*yue 6

更改

android:layout_width="wrap_content"
Run Code Online (Sandbox Code Playgroud)

android:layout_width="match_parent"
// or 
android:layout_width="0dp"
Run Code Online (Sandbox Code Playgroud)

TextView的宽度。


npk*_*npk 5

设置宽度TextView0dp

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    ... />
Run Code Online (Sandbox Code Playgroud)