2017-08-09 95 views
-3

这是预览 this is the preview links 和我的布局代码如下textViews具有相同TEXTSIZE但显示在TEXTSIZE不同,为什么

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

正如你看到的,这里是二的TextView有相同的文字和同样的textSize,但它显示不同的文字大小,我不明白为什么,谁可以给我一个解释,谢谢。如果 有错误在MixedTextView,请给我正确的代码谢谢。下面 是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> 
+1

您对第二文本使用CustomTextView(MixedTextView)。 –

+1

为什么您的MixedTextView扩展了LinearLayout?根据我的TextView扩展视图,改变它并且再次运行你的代码。 –

+0

如果你的世界喜欢显示相同的文本,然后使用相同的TextView的MixedTextView或TextView。 –

回答

0

通过练习验证,正确的答案在评论列表5中: setTextSize()方法期望参数为缩放像素(sp),但l通过了直线像素。所以在这里我需要 在MixedTextView中将setTextSize()调用更改为setTextSize(TypedValue.COMPLEX_UNIT_PX,size),谢谢@Mike M

-1

的代码的原因是你的文本样式或字体样式两种文字different.The一个是的TextView,另一种是com.asop.MixedTextView。我希望你明白我的观点。

+3

这应该是评论。 –

+0

那么需要什么downvote –

+0

这是不是由我投票。 –

相关问题