首页 > 解决方案 > 嵌套中的 TextView (ConstraintLayout --> LinearLayout) 仅在编辑器中可见

问题描述

包含在 LinearLayout 中的 ConstraintLayout 中的 TextView 仅在 Android Studio 的预览窗口中可见。

TextView 被限制在父级的开始和顶部,所以它应该放在 View 的左上角。所有三个视图都设置为填充父级的大小。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <android.support.constraint.ConstraintLayout
        android:id="@+id/view_downloads"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"> <!-- default -->

        <TextView
            android:id="@+id/textview_downloads"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="@android:style/TextAppearance.Material.Large"
            android:visibility="visible"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Hello World!" />

    </android.support.constraint.ConstraintLayout>

</LinearLayout>

我希望看到“Hello World!” 放置在屏幕的左上角,如此处的预览窗口所示,但我看到的是一个空白视图,如此屏幕截图所示,取自使用“空活动”项目模板和所示布局的最小示例多于。

标签: androidandroid-layouttextview

解决方案


尝试使用

android:text="Hello World!"

代替

tools:text="Hello World!"

我猜 Android Studio 已经停止发出这些警告。

注意: tools:text仅在布局编辑器中用于标记事物

另一个注意事项:您可以TextView.setText()在您的活动中使用作为替代。


推荐阅读