2016-07-07 61 views
0

enter image description here如何讓我的TextView定位響應?

在圖像上方,黑色語音氣泡和它後面的紅色背景是單個ImageView跨越垂直電話屏幕的寬度。 「你好」是TextView,佈局是相對的。使用邊距,我可以在Android模擬器中將語音氣泡中的「Hello」定位 - 但是當我模擬不同的手機時,定位關閉。

  1. 有沒有更好的方式來定位我TextView
  2. 是有辦法使它響應(這樣你好總是在 講話氣泡,無論什麼設備)?

這裏是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/relativeLayout"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/mainGraphic" 
     android:src="@drawable/finished2" 
     android:layout_marginBottom="97dp" 
     android:layout_above="@+id/enterValue" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:maxWidth="200dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/helloText" 
     android:textColor="#FFFFFF" 
     android:layout_marginEnd="29dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginTop="45dp" /> 

</RelativeLayout> 
+0

是9補丁圖片嗎? –

回答

0

我會用一個的FrameLayout代替RelativeLayout的有相似的圖像大小相同。 然後使用TextView的屬性android:layout_gravity="center"android:gravity="center"

0

試試這個:

... 
    <TextView 
     android:maxWidth="200dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/helloText" 
     android:textColor="#FFFFFF" 
     android:layout_alignTop="@+id/mainGraphic" 
     android:layout_alignBottom="@+id/mainGraphic" 
     android:layout_alignLeft="@+id/mainGraphic" 
     android:layout_alignRight="@+id/mainGraphic" 
     android:gravity="center" /> 
... 
0

最好的辦法是使用9修補圖像&將其設置爲TextView的背景。如果您使用普通的PNG文件,我們不能保證它將在所有分辨率上正確對齊,在延伸時它可能會變得模糊不清。

9-Patch圖像自動調整大小以適應視圖的內容和屏幕的大小。 Android SDK提供了一個工具,用於創建位於SDK sdk/tools目錄中的9-patch圖像。請參閱https://developer.android.com/studio/write/draw9patch.html

0

對於消息形狀,您應該使用9補丁。你可以做的是將你的佈局的根視圖作爲FrameLayout,然後向它添加一個TextView。設置消息形狀九補丁作爲TextView的背景。

用於創建Roman Nurik的9修補程序訪問https://romannurik.github.io/AndroidAssetStudio/nine-patches.html。只需上傳圖片資源的png文件,然後選擇定義以獲得最佳效果。 Nine-patch有3個主要屬性 -

拉伸區域:定義資產的哪些區域可以拉伸以適應不同的屏幕密度。

內容填充:定義將出現在圖像內部的內容(文本)的填充區域。

光學邊界:定義在資產周圍應光學可見多少區域。

優點使用九補丁:

  • 可擴展
  • 使用方便
  • 輕型
  • 中的任意內容量可放於它。

希望這會有所幫助。

+0

謝謝你的回答! 9補丁是棘手的,因爲它只會呈現一定的大小(由於相對佈局?),我仍然不得不在補丁中文本應該開始的xml眼睛。但它確實改善了這個問題。 – callynx

+0

我不明白你的意思,「它只會呈現一定的尺寸(由於相對佈局?)」。您不應該在根佈局中遇到任何問題。你能明確指出你在做什麼嗎?謝謝 – Siddharth2092

+0

下面是一個快速截圖:http://imgur.com/DbYiS1Q。文字氣泡只想要這個尺寸,否則填滿整個屏幕寬度。而我需要它是這個寬度,但是2/3這個高度。但它不會調整這種方式。 代碼: <?xml version =「1.0」encoding =「utf-8」?> callynx

0

理想情況下,您應該爲RelativeLayout使用完整的紅色背景,併爲TextView使用黑色語音9patch圖像。 但是,如果您確實希望RelativeLayout背景爲帶有氣泡的紅色,則必須在運行時將其設置爲紅色。

計算RelativeLayout在運行時的高度和寬度,分別爲150px300px。 仔細查看背景圖像,並確定從頂部,右側,底部和左側的氣泡邊緣點。例如,如果圖像高度爲100px,200px,並且氣泡頂部開始於30px並且底部爲70px,則右邊緣開始於150px並且左側結束於250px。還要計算TextView的寬度和高度。

在運行時,根據上圖改變TextView的位置。