簡體   English   中英

Android:為什么我的第二個TextView不顯示,而第一個顯示?

[英]Android: Why doesn't my second TextView show up, while the first one does?

我添加了第二個TextView ,它應該在第一個TextView下面,但現在第一個TextView正在工作,但第二個沒有顯示。

這是我的代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/state1"
android:textSize="16sp" />

<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_below="@id/textView1"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/sayer1"
android:textSize="12sp" />


<TextView
android:id="@+id/thirdLine"
android:layout_below="@+id/textView2" 
android:text="@string/state2"
android:textSize="16sp" />

<TextView
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" 
android:text="@string/sayer2"
android:textSize="16sp" />

</RelativeLayout>

可能是您的頂級RelativeLayout的高度設置為?android:attr/listPreferredItemHeight並且第一個文本視圖高度設置為包裝內容。 根據第一個文本視圖的高度,可能只是布局不夠高,無法顯示兩者

將頂部RelativeLayout更改為使用wrap_content的高度

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip" >

看看這兩個文本視圖:

<TextView
android:id="@+id/thirdLine"
android:layout_below="@+id/textView2" 
android:text="@string/state2"
android:textSize="16sp" />

<TextView
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" 
android:text="@string/sayer2"
android:textSize="16sp" />

這將無法正常工作,因為您將第二個放在底部並使用layout_alignParentBottom="true" ,然后您嘗試在其下面放置一些東西。 但它與底部對齊,因此它下方沒有空間!

將這兩個底部文本視圖更改為以下可能會執行您想要的操作:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:text="@string/sayer2"
android:textSize="16sp" />

<TextView
android:id="@+id/thirdLine"
android:text="@string/state2"
android:textSize="16sp" />

</LinearLayout>

你需要三件事來讓別人出現。

第一:

將此屬性更改為android:layout_height="?android:attr/listPreferredItemHeight"改為android:width="match_parent"

第二:

設置TextView的寬度/高度。 thirdLinetextView2沒有一個適當的寬度/高度。 嘗試做到這一點:

android:layout_width="match_parent"
android:layout_height="wrap_content"  

最后:

不要嘗試將TextView放在另一個對齊為alignParentBottom 而不是這樣,嘗試添加一個容器,其對齊位於全局父級的底部。 見下文:

試試這種格式:

<?xml version="1.0" encoding="utf-8"?>
// set a height to wrap_content/match_parent/fill_parent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="6dip" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="state1"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_below="@id/textView1"
        android:ellipsize="end"
        android:singleLine="true"
        android:lines="1"
        android:text="sayer1"
        android:textSize="12sp" />

    // if you want to align the latter text
    // to the bottom, make a container as...
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        // declare the width/height of the TextView
        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="26dip"
            android:text="sayer2"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/thirdLine"
            android:layout_width="fill_parent"
            android:layout_height="26dip"
            android:layout_below="@+id/textView2" 
            android:text="state2"
            android:textSize="16sp" />

    </RelativeLayout>

</RelativeLayout>  

如果這是預期的結果,請告訴我。 希望這可以幫助。

注意 :我被告知這個答案是錯誤的,但它確實突出了布局模擬器解析器中的一個問題。 從這個角度來看,我將把這個答案留下來記錄這個漏洞。


您可以通過以下方式將@+id/TextView2添加到下面的布局中。

android:layout_below="@+id/textView2"

請嘗試使用此參考具體ID,您需要添加大小調整參數

<TextView
  android:id="@+id/thirdLine"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/textView2" 
  android:text="@string/state2"
  android:textSize="16sp" />

這只適用於我的android布局模擬器中的這些更改,但您可能需要將定義textView2移動到它引用的位置之上。

這是在我的布局構建器中工作的文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="asdf"
android:textSize="16sp" />

<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_below="@id/textView1"
android:ellipsize="marquee"
android:singleLine="true"
android:text="fdsa"
android:textSize="12sp" />


<TextView
android:id="@+id/thirdLine"
android:layout_below="@id/textView2" 
android:text="asdffdsa"
android:textSize="16sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" 
android:text="string/sayer2"
android:textSize="16sp" />

</RelativeLayout>

我不認為你的布局工作正常,你應該得到一個“沒有資源找到...”對@ id / textView2,因為你試圖引用它

android:layout_below="@id/textView2"

你的“thirdLine”的TextView中之前 ,您已經添加了textView2到RelativeLayout的。

訂單應該是這樣的:

<TextView
android:id="@+id/textView1"" />

<TextView
android:id="@+id/secondLine"
android:layout_below="@id/textView1" />

<TextView
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" " />

<TextView
android:id="@+id/thirdLine"
android:layout_below="@+id/textView2" />

請記住,當你有RelativeLayout時,你添加你的觀點的順序並不重要,你可以告訴他們在哪里定位自己。

必須經常發生的是,如果您引用視圖,它必須位於引用它的視圖之上 哇這令人困惑。

假設A想要低於B.然后在你的RelativeLayout中,B必須在A之前到來。

現在已經解釋了。 在你textView2你告訴它,這樣它進入底部對齊本身對其父。 當然,你不能有低於textView2 thirdLine因為textView2底部靠左對齊。 嘗試改為:

<TextView
android:id="@+id/thirdLine"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />

<TextView
android:id="@+id/textView2"
android:layout_above="@+id/thirdLine" />

此外,IDE是否告訴您說明“layout_width”和“layout_height”? 對於TextViews。 如果這根本沒有幫助你,請你畫一張你需要的小圖片,以便我們更好地指導你?

我還建議將父視圖(Your RelativeLayout)的高度和寬度說明為“fill_parent”(API 7及以下)或“match_parent”(API 8及以上版本)。

對於我想去的其他觀點

android:layout_width="match_parent" or "fill_parent"
android:layout_height="wrap_content"

但是你也可以將寬度設置為“wrap_content”,更適合你的UI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM