Android布局:包装两个textview

问题描述 投票:9回答:5

我想实现以下布局

AAAAAAAAAAAAAAAAAA
AAAAA... BBBBBBBBB

AAAA是textview(ellipsize = "end"),BBB也是textview。它永远不会超过两行。我只需要将第一个textview包裹在第二种类型中。

我该怎么办?

android textview
5个回答
1
投票

最后结果

说明

它绝不是完美的,可能需要进行一些调整才能满足您的需求。我省略了ellipsize部分以使其更通用,但是很容易调整代码。这个想法很简单,但我花了几个小时(仅因为它的星期天)才能让它发挥作用。我们计算第二个TextView中多少个字符符合第一个字符的最后一行。然后使用该数字,我们将最后一行添加到第二个TextView,颜色与容器的背景相匹配,这是一个限制,可能以某种方式解决但我似乎无法找到它。

有了这个,我们将两个视图的文本对齐,但它们仍然没有相互重叠以允许包装效果。所以我们得到文本的高度,并为第二个添加一个负边距,使它们重叠,以确保第一个TextView位于第二个bringToFront()之上,我们需要调用RelativeLayout以避免文本具有相同的颜色第一个最后一行的背景(哇这令人困惑)。它可能不适用于不同的字体和权重(例如粗体),因为字符可能会占用更多空间。

我没有把它变成一种方法,但它很容易变得通用和可重复使用,只是想证明我是否可以做到。这是任何人和每个人享受和改进的代码。布局只是一个容器(例如first)和两个带有ids second 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); } } }); 的文本视图,我在第二个上将文本颜色设置为红色以使其更清晰但不是必需的。

<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>

1
投票

在RelativeLayout中,使textview AAA正常的2行高文本视图没有椭圆化,并使文本视图BBB与文本“... BBBBBBBB”和非透明背景,因此文本视图AAA将不会在BBB后面可见。

第二个选项是以编程方式执行 - 计算字母数量,如果长度超过应该将字符串减少。

但是,如果你不使用等宽字体,这两个“黑客”中的任何一个都不会100%工作。对不起我的英语不好。


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);

您还需要计算AAA中的字母数量,以及BBB中的字母数量。如果这两个金额的总和超过允许(文本将重叠),则在BBB字符串之前添加“...”字符串:

http://docs.oracle.com/javase/8/docs/api/java/awt/FontMetrics.html

0
投票

最完美和精确的解决方案只能通过以下方式实现:FontMetrics <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> 通过这个类,我们可以测量字符,字符串等。


-1
投票

没有使用任何库。

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

-1
投票

你可以使用HTML

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.