2016-08-30 91 views
4

我想創建一個類似於iMessage的佈局的消息傳遞視圖,其中聊天「泡沫」是其內容的大小,直到它獲取是一定的寬度。像這樣: screenshotNativescript:使標籤跨度寬度的內容,然後包裝在最大寬度

有了nativescript,我無法找到一個適合這個的佈局。我嘗試過使用GridLayout,但列的自動屬性似乎意味着它將是內容的大小,即使內容的大小超出了視圖範圍。

<GridLayout width="100%" columns="40, auto" rows="auto, 20" class="msg them" visibility="{{is_me ? 'collapsed' : 'visible'}}"> 
    <Image class="authorimg" col="0" stretch="aspectFill" verticalAlignment="top" src="{{user.profile_photo ? user.profile_photo.sizes.tiny_square : ''}}" /> 
    <StackLayout class="msg_text" col="1"> 
     <Label text="{{message}}" textWrap="true" verticalAlignment="top" /> 
    </StackLayout> 
    <Label class="msg_timestamp" text="{{author}}" verticalAlignment="top" row="1" colSpan="2" /> 
</GridLayout> 

產生這樣的: screenshot

通知的較長的不換,儘管textWrap =真上的標籤。

硬幣的另一面是這樣的:

<GridLayout width="100%" columns="40, *" rows="auto, 20" class="msg them" visibility="{{is_me ? 'collapsed' : 'visible'}}"> 
    <Image class="authorimg" col="0" stretch="aspectFill" verticalAlignment="top" src="{{user.profile_photo ? user.profile_photo.sizes.tiny_square : ''}}" /> 
    <StackLayout class="msg_text" col="1"> 
     <Label text="{{message}}" textWrap="true" verticalAlignment="top" /> 
    </StackLayout> 
    <Label class="msg_timestamp" text="{{author}}" verticalAlignment="top" row="1" colSpan="2" /> 
</GridLayout> 

唯一的區別是在GridLayout的列,在這種情況下其設置爲*(使用可用區域的其餘部分)。也就是說然而,產生這樣的:

screenshot

注意,短消息跨越整個寬度。我想在標籤上我需要類似width =「auto」的東西。我無法弄清楚這樣一種佈局,它可以適應兩種實現方式中最好的一種,小文本的小氣泡和長文本的氣泡。

+0

嘿,你有沒有嘗試在你的gridLayout中創建另一列? Something like columns =「40,*,*」 – Kansen

+0

@Kansen我確實嘗試過!對第二列(帶有消息標籤的列)使用*會使Label跨越列的整個寬度。然後在那裏添加另一個只是分隔這兩列之間的剩餘空間,產生這個:https://d3vv6lp55qjaqc.cloudfront.net/items/2E1S2k0C3S2t1s233T3j/Screenshot%202016-08-31%2008.42.59.png?X-CloudApp- Visitor-Id = 196928 – davecoffin

回答

0

你能嘗試用下面的代碼

<GridLayout width="100%" columns="40, *" rows="auto, 20" class="msg them" visibility="{{is_me ? 'collapsed' : 'visible'}}"> 
<Image class="authorimg" col="0" stretch="aspectFill" verticalAlignment="top" src="{{user.profile_photo ? user.profile_photo.sizes.tiny_square : ''}}" /> 
<StackLayout class="msg_text" col="1"> 
    <Textview editable="false" text="{{message}}" textWrap="true" verticalAlignment="top" /> 
</StackLayout> 
<Label class="msg_timestamp" text="{{author}}" verticalAlignment="top" row="1" colSpan="2" /> 

的事情是,標籤標記是一個行顯示,而的TextView允許你幾行顯示。

我希望這有助於你, 乾杯

+0

這不工作。 TextView跨越整個列的長度,當列是*時,它是視圖的其餘部分。另外,Label不僅僅用於一行顯示,textWrap =「true」屬性可以讓一個標籤跨越多行。使用TextView代替標籤結果如下:https://d3vv6lp55qjaqc.cloudfront.net/items/3F0w140i2f0J2Y0R1K3G/Screenshot%202016-08-31%2011.46.45.png?X-CloudApp-Visitor-Id=196928 – davecoffin

+0

然後我'我會嘗試我的最後一個項目符號: 如果您的柱設置如下:columns =「40,auto,*」 第一列設置爲40爲您的圖片 第二列與自動像這個列將獲得您的子組件的寬度(標籤/文本視圖) 第三列作爲明星填充剩餘空間 – Kansen

+0

我試過了,但它不起作用。自動列不限制在視圖中,它的大小與內容大小無關,不讓標籤換行。 – davecoffin

3

嘗試在網格包裹StackLayout。一個StackLayout測量它的一個邊到無窮大(在Veritcal方向,寬度是無限的,在水平方向,高度是無限的)。

在您的配置中,右邊界被測量爲無窮大,因此標籤沒有放置位置。

<GridLayout width="100%" columns="40, *" rows="auto, 20" class="msg them" visibility="{{is_me ? 'collapsed' : 'visible'}}"> 
<Image class="authorimg" col="0" stretch="aspectFill" verticalAlignment="top" src="{{user.profile_photo ? user.profile_photo.sizes.tiny_square : ''}}" /> 
<GridLayout col="1"> 
<StackLayout class="msg_text" > 
    <TextView editable="false" text="{{message}}" textWrap="true" verticalAlignment="top" /> 
</StackLayout> 
</GridLayout> 

解決您在保持較小的消息左對齊第二個問題,你設置爲左StackLayout新的網格佈局內對齊。這應該會產生結果,只有長消息纔會填充第二列的GridLayout所呈現的空間。

這裏有一個基本的例證: enter image description here

5

使用StackLayout將做的工作:

<StackLayout> 
    <StackLayout orientation="horizontal" style="padding: 5"> 
    <Image src="~/res/logo.png" width="50" height="50" verticalAlignment="top"/> 
    <Label text="I am a message" textWrap="true" backgroundColor="red" /> 
    </StackLayout> 
    <StackLayout orientation="horizontal" style="padding: 5"> 
    <Image src="~/res/logo.png" width="50" height="50" verticalAlignment="top"/> 
    <Label text="This is a longer message, which is worth adding because then I can make sure that longer messages display properly in the messages view! They need to wrap, obviously!" backgroundColor="red" textWrap="true" /> 
    </StackLayout> 
</StackLayout> 

你應該記住,佈局沒有虛擬化。當有大量對話時,這會讓你的應用程序變慢。您可以改爲使用列表視圖。

+0

謝謝!這終於奏效了。順便說一句,這是嵌入在列表視圖。我將在更新中詳細說明它是如何完成的,我贊成你的迴應。 – davecoffin