2017-09-22 27 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的工具,因此,所有的屬性您可以在工具中定義視圖元素 - 命名空間將在應用程序打包時自動剝離,並且不存在運行時間開銷。

這些屬性在佈局在工具中呈現時使用,但對運行時沒有影響。例如,如果您想要在編輯佈局時將示例數據放入文本字​​段中,但不希望這些屬性影響正在運行的應用程序,則這很有用。

希望這會有所幫助!

相關問題