2017-09-22 50 views
0

我正在尝试使用Android Studio和Firebase构建我的第一个Android应用程序。我已将它全部连接起来,并且它正在显示来自Firebase存储的数据库和图像的内容。问题是,出于某种原因,我的文本没有显示出我添加到xml中。在帖子的底部有3个按钮,“like”,“comment”和“re-post”,他们有一个图标,然后是文本旁边的文字。图标显示完美,但文字不会显示出来。 这里是“include_post_actions.xml”问题出在哪里?文字没有显示在Android上,但在Android工作室的预览中显示

<?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

回答

5

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

tools命名空间仅用于编辑的目的,是一种很好的方式来对齐事物而无需实际设置值。但是,如果要实际显示文本,则需要使用android名称空间。

+1

真棒,非常感谢你! –

+0

工作就像一个魅力!非常感谢! –

-1

尝试从match_parents变更宽度属性wrap_contents。

+0

我把所有东西都设置为wrap_content,但它没有显示出来,所以我想也许明确地给它一个更大宽度的match_parent可以工作,但都没有工作 –

1

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

+0

谢谢,我不知道 –

1

尝试改变tools:text="Repost"android:text="Repost"

按照文档:

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

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

希望这会有所帮助!

相关问题