【问题标题】:TextView in nested (ConstraintLayout --> LinearLayout) visible only in editor [duplicate]嵌套的TextView(ConstraintLayout-> LinearLayout)仅在编辑器中可见[重复]
【发布时间】:2019-06-26 17:23:41
【问题描述】:

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!”放置在屏幕的左上角,如Preview window here 所示,但我看到的是一个空白视图,如this screenshot 所示,取自使用“空活动”项目模板和布局的最小示例如上所示。

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    尝试使用

    android:text="Hello World!"
    

    而不是

    tools:text="Hello World!"
    

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

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

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

    【讨论】:

    • 对,谢谢,在用文字替换资源后忘记了它们是单独的属性。我相当肯定在我切换资源之前发生了同样的问题
    • 再想一想,这可能是stackoverflow.com/questions/40983459的副本
    • @HewwoCraziness 我不认为这是重复的。这里的问题是您使用了tools 命名空间。
    • 没关系。你可能是对的。
    • 切换到 Android 命名空间有效。谢谢。 (标记为重复。)
    猜你喜欢
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2017-01-10
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    相关资源
    最近更新 更多