這是預覽 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>
您對第二文本使用CustomTextView(MixedTextView)。 –
爲什麼您的MixedTextView擴展了LinearLayout?根據我的TextView擴展視圖,改變它並且再次運行你的代碼。 –
如果你的世界喜歡顯示相同的文本,然後使用相同的TextView的MixedTextView或TextView。 –