2013-10-23 46 views
0

我在Android中創建佈局時遇到了問題。Android - TextView忽略「wrap_content」

我有一個線性佈局,應該從上到下顯示一個TextView,EditView和Button。 問題是,當我將TextView的width和height屬性設置爲「wrap_content」時,它的行爲與「fill_parent」相似,並填充整個屏幕的寬度和高度。 作爲結論,其他兩個元素(EditView和Button)將不會顯示。

我只是無法弄清楚我做錯了什麼。

這裏是我的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/app_background" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/text_nickname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="15dp" 
     android:gravity="center_horizontal" 
     android:text="@string/label_nickname" 
     android:textColor="#000000" 
     android:textSize="15sp" /> 

    <EditText 
     android:id="@+id/edit_nickname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="@string/hint_insert_nickname" 
     android:shadowColor="#000000" 
     android:shadowDx="-1" 
     android:shadowDy="-1" 
     android:shadowRadius="1" 
     android:textSize="15sp" /> 

    <Button 
     android:id="@+id/button_play" 
     style="@style/MainButtonTheme" 
     android:layout_marginTop="25dp" 
     android:onClick="click_playButton" 
     android:text="@string/button_start" /> 
</LinearLayout> 

期待您的輸入。

許多問候的

Siggy

編輯:

MainButtonStyle:

<style name="MainButtonTheme"> 
    <item name="android:background">@drawable/states_menu_button</item> 
    <item name="android:layout_width">wrap_content</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:textSize">25sp</item> 
    <item name="android:textColor">#000000</item> 
    <item name="android:textStyle">bold</item> 
</style> 

編輯#2:

下面是它看起來像現在截圖什麼是期望的:

當前狀態:

enter image description here

期望狀態:

enter image description here

我測試了它在手機上,它看起來像這樣的手機上使用。

+0

如果我運行代碼,則不會發生這種情況。如果沒有答案可以幫助你,你能否嘗試檢查視圖層次結構和其他工具來追蹤這些按鈕和編輯文本的位置?張貼他們的快照。 – kishu27

+0

嘿,我嘗試了你的佈局,它在模擬器中顯示得很好,你能告訴我你的輸出,你得到了什麼? –

+0

我添加了2個截圖。我在手機上測試了它,它看起來完全像仿真器一樣。這兩個元素被textview推出可見屏幕:/ – Siggy

回答

1

好吧,我發現了這個問題。 這對我來說似乎是一個錯誤。

在我的style文件我已經定義了這樣整個應用程序背景:

<style name="AppTheme" parent="AppBaseTheme"> 
    <item name="android:background">@drawable/app_background</item> 
    </style> 

當我刪除以下行:

<item name="android:background">@drawable/app_background</item> 

與WRAP_CONTENT TextView的行爲就像它應該。

我真的不知道背景圖片與它有什麼關係。 會很高興,如果有人能告訴我:)

問候

Siggy

-1

試過這與相對佈局,它爲我工作,看看是否有幫助。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#F88017" 
    > 


    <TextView 
     android:layout_centerHorizontal="true" 
     android:id="@+id/text_nickname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="15dp" 
     android:text="Nickname" 
     android:textColor="#000000" 
     android:textSize="15sp" /> 

    <EditText 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="10dp" 
     android:layout_below="@+id/text_nickname" 
     android:id="@+id/edit_nickname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="insert_nickname" 
     android:shadowColor="#000000" 
     android:shadowDx="-1" 
     android:shadowDy="-1" 
     android:shadowRadius="1" 
     android:textSize="15sp" /> 

    <Button 
      android:layout_centerHorizontal="true" 
     android:layout_below="@+id/edit_nickname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="25dp" 
     android:onClick="click_playButton" 
     android:text="Start" 
     android:textColor="#000000" 
     android:textSize="25dp" 
     android:textStyle="bold" /> 

</RelativeLayout> 
+0

不幸的是它不起作用。文本視圖只顯示字符串「暱稱」。所以這不應該太多;) – Siggy

+0

你可以發佈一個當前的快照和什麼是想要的? –

+0

添加到我的問題。如果我使用你的代碼,我可以看到其他2個元素(編輯和按鈕),如果我向下滾動大約2個屏幕長度... – Siggy

0

試試這個代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 

       android:gravity="center_horizontal" 
       android:orientation="vertical" > 

    <TextView 
      android:id="@+id/text_nickname" 
      android:layout_width="10dip" 
      android:layout_height="10dip" 
      android:layout_marginTop="15dp" 
      android:gravity="center_horizontal" 

      android:textColor="#000000" 
      android:textSize="15sp" /> 

    <EditText 
      android:id="@+id/edit_nickname" 
      android:layout_width="300dp" 
      android:layout_height="wrap_content" 

      android:shadowColor="#000000" 
      android:shadowDx="-1" 
      android:shadowDy="-1" 
      android:shadowRadius="1" 
      android:textSize="15sp" android:layout_marginLeft="3dp"/> 

    <Button 
      android:id="@+id/button_play" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_marginTop="25dp" 
      android:onClick="click_playButton" 
      android:text="Button" /> 
</LinearLayout> 
0

試試吧,我做了一些改動即刪除樣式屬性:

<TextView 
    android:id="@+id/text_nickname" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="15dp" 
    android:gravity="center_horizontal" 
    android:text="label_nickname" 
    android:textColor="#000000" 
    android:textSize="15sp" /> 

<EditText 
    android:id="@+id/edit_nickname" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="hint_insert_nickname" 
    android:shadowColor="#000000" 
    android:shadowDx="-1" 
    android:shadowDy="-1" 
    android:shadowRadius="1" 
    android:textSize="15sp" /> 

<Button 
    android:textSize="25dp" 
    android:id="@+id/button_play" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="25dp" 
    android:onClick="click_playButton" 
    android:text="button_start" /> 

0
// try this 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/app_background" 
    android:padding="5dp" 
    android:orientation="vertical" 
    android:gravity="center"> 
    <LinearLayout 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1"> 
     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:padding="5dp" 
       android:orientation="vertical" > 

       <TextView 
        android:id="@+id/text_nickname" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="15dp" 
        android:text="@string/label_nickname" 
        android:textColor="#000000" 
        android:textSize="15sp" /> 

       <EditText 
        android:id="@+id/edit_nickname" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="top" 
        android:layout_marginTop="10dp" 
        android:hint="@string/hint_insert_nickname" 
        android:shadowColor="#000000" 
        android:shadowDx="-1" 
        android:shadowDy="-1" 
        android:shadowRadius="1" 
        android:textSize="15sp" /> 


      </LinearLayout> 
     </ScrollView> 
    </LinearLayout> 
    <Button 
     android:id="@+id/button_play" 
     style="@style/MainButtonTheme" 
     android:onClick="click_playButton" 
     android:padding="5dp" 
     android:text="@string/button_start" /> 
</LinearLayout> 
1

正如我找到了解決這一點,並沒有正確的答案。

問題是,因爲您使用了:name =「android:background」 它不是整個應用程序的背景,而是每個View的背景。你應該使用:name =「android:windowBackground」來爲app設置背景。