android之标签icon在文本多行背后或首次位置显示

本文介绍了一种算法,用于计算和调整两个TextView中的文字显示。通过测量文字宽度和高度,确定文字在视图中的最佳布局方式,确保文字不会超出指定区域,并保持良好的视觉效果。
private void drawImageViewDone(int width, int height) {
		try
		{
			textWidth = content_top_tv.getTextSize();
			paint.setTextSize(textWidth);
			// 一行字体的高度
			int lineHeight = content_top_tv.getLineHeight();
			// 可以放多少行
			int lineCount = (int) height / lineHeight;
			// int lineCount = 4;
			// 一行的宽度
			float rowWidth = mScreenWidth - width - content_top_tv.getPaddingLeft()
					- content_top_tv.getPaddingRight();
			// 一行可以放多少个字
			int columnCount = (int) (rowWidth / textWidth);

			// 总共字体数等于 行数*每行个数
			count = lineCount * columnCount;
			if(text.length() < count)
			{
				content_top_tv.setText(text);
				content_bottom_tv.setVisibility(View.GONE);
				return;
			}
			// 一个TextView中所有字符串的宽度和(字体数*每个字的宽度)
			textTotalWidth = (float) ((float) count * textWidth);

			measureText();
			content_top_tv.setText(text.substring(0, count));

			// 检查行数是否大于设定的行数,如果大于的话,就每次减少一个字符,重新计算行数与设定的一致
			while (content_top_tv.getLineCount() > lineCount) {
				count -= 1;
				content_top_tv.setText(text.substring(0, count));
			}
			
			//content_bottom_tv.setPadding(0, lineCount * lineHeight - height, 0, 0);
			//gz此处修改将原有布局中marginTop=8dp 删除,setPadding顶部取的值由  -8 改为 8, 从而解决textview顶部被砍掉的错乱问题
			
			content_bottom_tv.setPadding(0, height-lineCount * lineHeight , 0, 0);
			content_bottom_tv.setText(text.substring(count));
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}





xml:

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/preg_export_bg"
                    android:orientation="horizontal"
                    android:padding="6dip" >

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

                        <ImageView
                            android:id="@+id/epic"
                            android:layout_width="90dip"
                            android:layout_height="90dip"
                            android:layout_alignParentRight="true"
                            android:layout_marginRight="6dip"
                            android:layout_marginTop="5dip"
                            android:background="@drawable/default_user_head" />

                        <TextView
                            android:id="@+id/content_top_tv"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="6dip"
                            android:layout_marginTop="6dip"
                            android:layout_toLeftOf="@+id/epic"
                            android:lineSpacingExtra="4dip"
                            android:textColor="@color/gray3"
                             android:text="打算发送到发送到发送到发送到发送到发送到发送到发的说法送到发送到asdfasdf发的说法gadf "
                            android:textSize="16sp" />

                        <TextView
                            android:id="@+id/content_bottom_tv"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/epic"
                            android:layout_marginLeft="6dip"
                            android:layout_marginRight="6dip"
                            android:lineSpacingExtra="4dip"
                            android:textColor="@color/gray3"
                            android:text="打算发送到发送到发送到发送到发送到发送到发送到发的说法"
                            android:textSize="16sp" />
                    </RelativeLayout>
                </LinearLayout>


demo :点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值