簡體   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