繁体   English   中英

使用XML在Constraint Layout中为TextView添加边距

[英]Adding margin to TextView in Constraint Layout using XML

我想在TextView Widget中添加左右边距,但它似乎不起作用。

这是我的活动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。

更改

android:layout_width="wrap_content"

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

作为TextView的宽度。

TextView宽度设置为0dp

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    ... />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM