我想实现以下布局
AAAAAAAAAAAAAAAAAA
AAAAA... BBBBBBBBB
AAAA是文本视图(ellipsize = "end"
),BBB也是文本视图。
它永远不会超过两行。我只需要将第一个textview包含在第二种文本视图中。
我该怎么做?
答案 0 :(得分:1)
在RelativeLayout中,使textview AAA正常的2行高文本视图没有椭圆化,并使用文本" ... BBBBBBBB"进行textview BBB。并且具有非透明背景,因此在BBB后面不会看到textview AAA。
第二个选项是以编程方式执行 - 计算字母数量,如果长度超过应该将字符串减少。
但是那些2" hacks"如果您不使用等宽字体,将以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中的字母数量。如果这两个金额的总和超过允许的数量(文本将重叠),请添加&#34; ...&#34; 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);
答案 1 :(得分: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);
}
}
});
的文本视图,我在第二个上将文本颜色设置为红色以使其更清晰但这没有必要。
<强>代码强>
{{1}}
答案 2 :(得分:0)
最完美和最精确的解决方案只能通过以下方式实现: FontMetrics http://docs.oracle.com/javase/8/docs/api/java/awt/FontMetrics.html 通过这个类,我们可以测量字符,字符串等。
答案 3 :(得分:-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>
答案 4 :(得分:-1)
您可以使用html
myTextView.setText(Html.fromHtml("<p>Any html code</p>"));