-3

this is the preview this is the preview links and my layout code is below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.asop.MainActivity">

<TextView
    android:id="@+id/tv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="这是一段测试文字"
    android:textSize="20sp" />

<com.asop.MixedTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:text1="这是一段测试文字"
    app:text1size="20sp" />

as you see,here is two textView with same text and same textSize,but it shows different in textsize,l dont understand why,who can give me a explain,thank.if there is errors in MixedTextView ,please give me the right code thanks. below is the code of MixedTextView

public class MixedTextView extends LinearLayout {
private static final int TEXTSIZE = 16;
private static final int TEXTCOLOR = R.color.normal;
private static final int DEVIDER_LENGTH = 5;
private String text1;
private String text2;
private int text1Color;
private int text2Color;
private int text1Size;
private int text2Size;
private int deviderLength;
private TextView tv1, tv2;

public MixedTextView(Context context) {
    this(context, null);
}

public MixedTextView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
}

public MixedTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MixedTextView);
    text1 = ta.getString(R.styleable.MixedTextView_text1);
    text2 = ta.getString(R.styleable.MixedTextView_text2);
    text1Size = ta.getDimensionPixelSize(R.styleable.MixedTextView_text1size, TEXTSIZE);
    text2Size = ta.getDimensionPixelSize(R.styleable.MixedTextView_text2size, TEXTSIZE);
    text1Color = ta.getColor(R.styleable.MixedTextView_text1color, getResources().getColor(TEXTCOLOR));
    text2Color = ta.getColor(R.styleable.MixedTextView_text2color, getResources().getColor(TEXTCOLOR));
    deviderLength = ta.getDimensionPixelSize(R.styleable.MixedTextView_deviderLength, DEVIDER_LENGTH);
    ta.recycle();
    initView(context);
}

private void initView(Context context) {
    tv1 = new TextView(context);
    tv1.setSingleLine();
    tv1.setText(text1);
    tv1.setTextSize(text1Size);
    tv1.setTextColor(text1Color);

    tv2 = new TextView(context);
    tv2.setSingleLine();
    tv2.setText(text2);
    tv2.setTextSize(text2Size);
    tv2.setTextColor(text2Color);

    View devider = new View(context);
    LinearLayout.LayoutParams deviderParams = new LinearLayout.LayoutParams(deviderLength, 1);
    if (getOrientation() == VERTICAL)
        deviderParams = new LinearLayout.LayoutParams(1, deviderLength);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    addView(tv1, layoutParams);
    addView(devider, deviderParams);
    addView(tv2, layoutParams);
}
<declare-styleable name="MixedTextView">
    <attr name="text2" format="string" />
    <attr name="text1" format="string" />
    <attr name="text1color" format="color|reference" />
    <attr name="text2color" format="color|reference" />
    <attr name="text1size" format="dimension|reference" />
    <attr name="text2size" format="dimension|reference" />
    <attr name="deviderLength" format="dimension|reference" />
</declare-styleable>
hai
  • 1
  • 3

2 Answers2

0

Verified by practice,the correct answer is in comment list 5: The setTextSize() method expects the argument to be in scaled pixels (sp), but l passed straight pixels. so here l need to Change the setTextSize() calls to setTextSize(TypedValue.COMPLEX_UNIT_PX, size) in MixedTextView, thanks @Mike M

hai
  • 1
  • 3
-1

The reason is your text style or font style for both the text is different.The one is TextView and the other one is com.asop.MixedTextView. I hope you get my point.

Deepak Rana
  • 539
  • 4
  • 18