2014-09-26 87 views
9

我想實現如下的佈局的Android佈局:裹2的TextView

AAAAAAAAAAAAAAAAAA 
AAAAA... BBBBBBBBB 

凡AAAA是一個TextView(ellipsize = 「端」)和BBB是一個TextView爲好。 它永遠不會超過兩行。我只需要將第一個textview包裹在第二個類中。

我該怎麼做? 感謝

編輯:增加了屏幕截圖

Idea in screenshot

+2

你的意思是你要根據定位BBB後留下的空間BBB到了一個充分展示和AAA包裹? – 2014-09-26 02:25:32

+0

Y絕對! – Stephane 2014-09-26 03:41:27

+0

我想我誤解了你,你的AAA是一個兩行的TextView,並根據BBB包裝在它的第二行,對吧?如果答案是肯定的,我認爲我不能提供幫助。 – 2014-09-26 04:02:21

回答

1

最終結果

enter image description here

說明

這是絕不是完美的,並可能需要一些調整,以適應您的需求。我遺漏了ellipsize部分以使其更通用,但很容易修改代碼。這個想法很簡單,但花了我幾個小時(只是因爲它的星期天)才讓它工作。我們計算第二個TextView多少個字符適合第一個字符的最後一行。然後用這個數字,我們將第二行TextView的最後一行加上與容器背景相匹配的顏色,這是一個可能以某種方式解決的限制,但我似乎無法找到它。因此,我們可以將兩個視圖的文本對齊,但它們仍然不會相互重疊以允許包裝效果。因此,我們得到文本的高度,併爲第二個文本添加負邊距,以使它們重疊,以確保第一個文本位於第二個文本的頂部,我們需要調用bringToFront()以避免文本的顏色與第一個最後一行的背景(這令人困惑)。它可能不適用不同的字體和粗細(例如粗體),因爲角色可能需要更多的空間。

我沒有使它成爲一種方法,但它可以很容易地變得通用和可重用,只是想證明我是否可以做到這一點。以下是任何人和每個人都可以享受和改進的代碼。佈局只是一個容器(例如RelativeLayout)和兩個文本查看與ID firstsecond,我已經設置文本顏色在第二個紅色,使其更清晰,但沒有必要。 的FontMetrics http://docs.oracle.com/javase/8/docs/api/java/awt/FontMetrics.html 有了這個類,我們可以測量字符,字符串等:

代碼

final TextView first = (TextView) findViewById(R.id.textView); 
    final TextView second = (TextView) findViewById(R.id.textView2); 
    final ViewGroup container = (ViewGroup)findViewById(R.id.container); 
    final ViewTreeObserver obs = findViewById(R.id.container).getViewTreeObserver(); 

    obs.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      //Get characters from the first TextView's last line 
      String lastLine = first.getText().toString().substring(first.getLayout().getLineStart(first.getLineCount() - 1)); 

      //Calculate the number of extra characters that will fit in the first one 
      int e = 0; //Extra characters 
      int lineCount = first.getLineCount(); 
      while (lineCount == first.getLineCount() && e < second.getText().toString().length() - 1) { 
       first.append(second.getText().toString().substring(e, e + 1)); 
       e++; 
      } 

      //Remove those characters again to leave the first TextView as it was 
      String firstString = first.getText().toString(); 
      first.setText(firstString.substring(0, firstString.length() - e)); 

      //Add the last line of the first textview to the second textview 
      second.setText(Html.fromHtml("<font color='#F2F2F2'>"+lastLine+"</font>"+second.getText())); 

      //add a negative margin to the second textview equal to a line's height 
      ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) second.getLayoutParams(); 
      params.topMargin = -(int)(first.getLineHeight()+10); 
      second.setLayoutParams(params); 

      //Make sure first is on top 
      first.bringToFront(); 

      //Remove the listener 
      if (Build.VERSION.SDK_INT < 16) { 
       container.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
      } else { 
       container.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      } 
     } 
    }); 
-1

使用任何圖書館出來。

<RelativeLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" > 

    <TextView 
     android:id="@+id/AAA" 
     android:layout_toLeftOf="@+id/BBB" 
     android:layout_height="wrap_content" 
     android:maxLines="2" 
     android:layout_width="match_parent" /> 

    <TextView 
     android:id="@+id/BBB" 
     android:layout_alignParentRight="true" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 
</RelativeLayout> 
+0

謝謝!它只是一條線。那兩條線呢? – Stephane 2014-09-26 04:10:51

+0

你的textview超過了兩行或者它已經是最大值了。 – waelhe 2014-09-26 04:14:10

+0

2行是要求。它可以佔用最多一行或兩行 – Stephane 2014-09-26 04:17:22

1

在RelativeLayout的讓TextView的AAA正常的2線高的TextView沒有ellipsization並TextView的BBB的文字「...... BBBBBBBB」,並與非透明背景,使TextView的AAA不會落後BBB可見。

第二個選項是以編程方式進行 - 計算字母的數量,如果長度超過應該的長度,則將該字符串剪下。

但是,如果您不使用等寬字體,那麼2個「黑客」都不會在100%的情況下工作。對不起我的英語不好。


<RelativeLayout android:layout_width="200dp" android:layout_height="70dp"> 

    <TextView android:id="@+id/aaa" 
    android:layout_width="match_parent" android:layout="match_parent" 
    android:lines="2" 
    android:typeface="monospace" /> 

    <TextView android:id="@+id/bbb" 
    android:layout_width="match_parent" android:layout="wrap_content" 
    android:typeface="monospace" 
    android:lines="1" 
    android:background="#ffffffff" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" /> 

</RelativeLayout> 

還需要在字符串AAA,因此在BBB計數的信件量。如果超過允許的2倍量的總和更BBB字符串之前(文本會重疊)添加 「...」 字符串:

BBBTextView = (TextView) findViewbyId(R.id.bbb); 

int maxLetterCount = 125; // you need to specify this number 
String AAAString = " gtgtfd def e refrfecj gjv jvrji rf wj jiefijr jgfrjf jrf" 
String BBBString = "deeded ed e ddd" 

if ((BBBString.length()+BBBString.length()) > maxLetterCount) { 
    BBBString = "... " + BBBString; 
} 

BBBTextView.setText(BBBString); 
+0

我使用roboto,因此它不是等寬的。我喜歡第一個想法。代碼是什麼樣的? – Stephane 2014-09-28 16:35:26

+0

你代表什麼代碼? – Incredible 2014-09-28 16:37:09

+0

xml代碼佈局 – Stephane 2014-09-28 16:38:05

0

最完美和精確的解決辦法只能是由於實現。

-1

您可以使用HTML

myTextView.setText(Html.fromHtml("<p>Any html code</p>")); 
+0

請提供一些解釋如何回答這個問題 – Huangism 2014-09-29 14:17:50