时间:2024-01-14 03:00:01 | 来源:网站运营
时间:2024-01-14 03:00:01 来源:网站运营
有android大神吗,做个小页面?:谢邀。public class MixtureTextView extends RelativeLayout { private ImageView imageContainer; private TextView rightText, bottomText; private CharSequence showText = ""; private boolean isSplited = true; private int splitedIndex = 0; public MixtureTextView(Context context){ this(context, null); } public MixtureTextView(Context context, AttributeSet attrs){ this(context, attrs, 0); } public MixtureTextView(Context context, AttributeSet attrs, int defStyle){ super(context, attrs, defStyle); // LayoutInflater.from(context).inflate(R.layout.text_around_image, this, true); imageContainer = (ImageView) findViewById(R.id.image_container); rightText = (TextView) findViewById(R.id.text_right); bottomText = (TextView)findViewById(R.id.text_bottom); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); // init(); } private void init(){ // int imgWidth = imageContainer.getWidth(); int imgHeight = imageContainer.getHeight(); // int textWidth = getWidth() - imgWidth; int textHeight = imgHeight; Log.d("MixtureTextView", "width: " + textWidth); Log.d("MixtureTextView", "height: " + textHeight); Log.d("MixtureTextView", "text height: " + rightText.getHeight()); if(isSplited){ // fix the chinese punctuation bug int realTextHeight = rightText.getHeight(); if(realTextHeight >= textHeight + 5){ //splitedIndex--; setText(showText, --splitedIndex); } return; } // int lineWidth = textWidth - rightText.getPaddingLeft() - rightText.getPaddingRight(); int lineHeight = rightText.getLineHeight(); int lineCount = (textHeight - rightText.getPaddingTop() - rightText.getPaddingBottom())/lineHeight; // Paint textPaint = rightText.getPaint(); int length = showText.length(); float curLineWidth = 0f; int curLineNumber = 0; splitedIndex = length; for(int i = 0; i < length; i++){ char curChar = showText.charAt(i); if(curChar == '/n'){ curLineNumber++; curLineWidth = 0f; }else{ float curCharWidth = textPaint.measureText("" + curChar); // 增加这个字符之后宽度超过行宽,另起一行 if(curLineWidth + curCharWidth > lineWidth){ curLineNumber++; curLineWidth = curCharWidth; }else { curLineWidth += curCharWidth; } } if(curLineNumber >= lineCount){ splitedIndex = i + 1; break; } } // setText(showText, splitedIndex); isSplited = true; } private void setText(CharSequence text, int splitedIndex){ if(splitedIndex >= text.length()){ rightText.setText(showText); bottomText.setVisibility(View.GONE); }else { CharSequence subText = showText.subSequence(0, splitedIndex); rightText.setText(subText); // subText = showText.subSequence(splitedIndex, text.length()); bottomText.setText(subText); bottomText.setVisibility(View.VISIBLE); } } /** * 公共接口,设置文本 * @param text */ public void setText(CharSequence text){ showText = text; isSplited = false; requestLayout(); }}
text_around_image.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image_container" android:layout_width="100dp" android:layout_height="100dp" android:paddingRight="10dp" android:paddingBottom="10dp" android:scaleType="fitCenter" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/text_right" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/image_container" android:layout_alignParentRight="true" android:text="右侧文本"/> <TextView android:id="@+id/text_bottom" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/image_container" android:text="底部文本"/></RelativeLayout>
关键词: