java - 文本未显示在 android 上,但在 android studio 中显示在预览中

标签 java android android-layout android-studio

我正在尝试使用 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 中的预览

enter image description here

模拟器中的应用... enter image description here

最佳答案

问题是您正在使用 tools:text="Repost"。那只显示在预览模式下,你需要使用 android:text="Repost" 才能真正显示它。

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

关于java - 文本未显示在 android 上,但在 android studio 中显示在预览中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46374064/

相关文章:

java - 在 Java 中将抽象文件转换为字符串

Android ListView 卡片行为

java - OpenCV,从图像中剪切 fragment

java - 如何在 Android 中通过名称获取图像资源?

android - RelativeLayout 中的堆叠按钮

java - Spring、JUnit、Hibernate 测试

java 抛出异常 java.lang.IndexOutOfBoundsException :

java - J2ME文本字段加倍

android - 如何像 centerCrop 那样从顶部缩放 ImageView?

android - 如何向 ShapeDrawable 添加描边