【问题标题】:Text not showing up on android, but does show in preview in android studio文本未显示在 android 上,但确实显示在 android studio 的预览中
【发布时间】:2017-09-22 22:31:12
【问题描述】:

我正在尝试使用 Android Studio 和 Firebase 构建我的第一个 Android 应用。我已将所有内容连接起来,它可以很好地显示数据库中的内容和 firebase 存储中的图像。问题是,由于某种原因,我添加到 xml 中的文本没有显示出来。在帖子的底部有 3 个按钮,“喜欢”、“评论”和“重新发布”,它们有一个图标,然后是它们旁边的文字。图标显示完美,但文本不会显示。 这是问题所在的“include_post_actions.xml”...

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_below="@+id/post_action_layout"
    android:layout_above="@+id/include"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:layout_weight="1"
    android:gravity="center_vertical">

    <LinearLayout
        android:id="@+id/post_action_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <com.like.LikeButton
                app:icon_type="heart"
                app:icon_size="18dp"
                android:id="@+id/star_button"
                android:layout_width="18dp"
                android:layout_height="18dp" />
            <TextView
                android:id="@+id/post_likes_count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textColor="@color/colorBlack"
                android:maxLines="1"
                tools:text="Like" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <ImageView
                android:id="@+id/post_comment_icon"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/ic_question_answer_black_24dp" />
            <TextView
                android:id="@+id/post_comments_count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textColor="@color/colorBlack"
                android:maxLines="1"
                tools:text="Comment" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <ImageView
                android:id="@+id/post_repost_icon"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/ic_autorenew_black_24dp" />
            <TextView
                android:id="@+id/post_repost_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textColor="@color/colorBlack"
                android:maxLines="1"
                tools:text="Repost" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/post_action_buttons">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginStart="20dp"
            android:gravity="center_vertical"
            tools:text="Likes Count"
            android:id="@+id/like_count_text"
            android:maxLines="1" />
    </LinearLayout>
    
</RelativeLayout>

在预览中,文本显示在图标旁边,但是当我在模拟器上运行它时,只有图标在那里,我不知道为什么。请帮忙。谢谢你。 Android Studio 中的预览

模拟器中的应用程序...

【问题讨论】:

    标签: java android android-layout android-studio


    【解决方案1】:

    问题是您使用的是tools:text="Repost"。那只在预览模式下显示,您需要使用android:text="Repost" 来实际显示它。

    tools 命名空间仅用于编辑器目的,是一种无需实际设置值即可对齐事物的好方法。但是,如果您想实际显示文本,则需要使用 android 命名空间。

    【讨论】:

    • 谢谢。出于某种原因,tools:text 潜入了我的 XML,我不知道为什么它没有显示。
    【解决方案2】:

    tools 属性引用允许您强制执行任何特定属性,仅用于在 Android Studio 中预览,以便您可以在编辑器中查看文本。通过android:text 设置文本将硬编码TextView 中的字符串。您也可以通过setText 方法在TextView 对象上更改文本

    【讨论】:

    • 谢谢,我不知道
    【解决方案3】:

    尝试将tools:text="Repost" 更改为android:text="Repost"

    根据文档:

    tools 命名空间是 Android 工具特别认可的命名空间,因此您在 tools-namespace 中定义的视图元素的所有属性将在应用程序打包时自动剥离,并且没有运行时开销。

    这些是在工具中呈现布局时使用的属性,但对运行时没有影响。例如,如果您希望在编辑布局时将示例数据放入文本字​​段中,但又不希望这些属性影响正在运行的应用,这将非常有用。

    希望这会有所帮助!

    【讨论】:

      【解决方案4】:

      尝试将宽度属性从 match_parents 更改为 wrap_contents。

      【讨论】:

      • 我将所有内容都设置为 wrap_content 但它没有显示出来,所以我想也许明确地给它一个更大的 match_parent 宽度会起作用,但没有一个起作用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多