Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 文本大小相同但显示文本大小不同的文本视图,为什么_Android - Fatal编程技术网

Android 文本大小相同但显示文本大小不同的文本视图,为什么

Android 文本大小相同但显示文本大小不同的文本视图,为什么,android,Android,这是预览 我的布局代码如下 <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="ma

这是预览 我的布局代码如下

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

正如你所看到的,这里有两个文本视图,具有相同的文本和文本大小,但显示的文本大小不同,我不明白为什么,谁能给我解释一下,谢谢 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>
public类MixedTextView扩展了LinearLayout{
私有静态最终int TEXTSIZE=16;
私有静态最终int TEXTCOLOR=R.color.normal;
专用静态最终内部设备长度=5;
私有字符串text1;
私有字符串text2;
私有int TEXT1颜色;
私有int text2Color;
私有int text1Size;
私有int text2Size;
私有int-deviderLength;
私有文本视图tv1、tv2;
公共混合文本视图(上下文){
这个(上下文,空);
}
public MixedTextView(上下文上下文,@Nullable AttributeSet attrs){
这(上下文,属性,0);
}
public MixedTextView(上下文上下文,@Nullable AttributeSet attrs,int defStyleAttr){
super(上下文、attrs、defStyleAttr);
TypedArray ta=context.getainstyledattributes(attrs,R.styleable.MixedTextView);
text1=ta.getString(R.styleable.MixedTextView\u text1);
text2=ta.getString(R.styleable.MixedTextView\u text2);
text1Size=ta.getDimensionPixelSize(R.styleable.MixedTextView\u text1Size,TEXTSIZE);
text2Size=ta.getDimensionPixelSize(R.styleable.MixedTextView\u text2Size,TEXTSIZE);
text1Color=ta.getColor(R.styleable.MixedTextView_text1Color,getResources().getColor(TEXTCOLOR));
text2Color=ta.getColor(R.styleable.MixedTextView_text2Color,getResources().getColor(TEXTCOLOR));
DeviceLength=ta.getDimensionPixelSize(R.styleable.MixedTextView\u DeviceLength,Devicer\u LENGTH);
ta.recycle();
initView(上下文);
}
私有void initView(上下文){
tv1=新文本视图(上下文);
tv1.设置单线();
tv1.setText(text1);
tv1.setTextSize(text1Size);
tv1.setTextColor(text1Color);
tv2=新文本视图(上下文);
tv2.setSingleLine();
tv2.setText(text2);
tv2.setTextSize(text2Size);
tv2.setTextColor(text2Color);
视图设备=新视图(上下文);
LinearLayout.LayoutParams设备参数=新的LinearLayout.LayoutParams(设备长度,1);
如果(getOrientation()==垂直)
DeviceParams=新的LinearLayout.LayoutParams(1,DeviceLength);
LinearLayout.LayoutParams LayoutParams=新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容);
addView(tv1,布局参数);
addView(devider、deviderParams);
addView(tv2,布局参数);
}

原因是您的文本样式或字体样式不同,因为两种文本都不同。一种是TextView,另一种是com.asop.MixedTextView。我希望您理解我的观点。

经实践验证,正确答案在注释列表5中: setTextSize()方法要求参数以缩放像素(sp)为单位,但我传递的是直像素
在MixedTextView中将setTextSize()调用更改为setTextSize(TypedValue.COMPLEX\u UNIT\u PX,size),谢谢您使用了CustomTextView(MixedTextView)对于第二个文本。为什么您的MixedTextView扩展了LinearLayout?根据我的说法,TextView扩展了视图,请更改该视图,然后再次运行代码。如果您希望显示相同的文本,请使用相同的TextView MixedTextView或TextView。可能与
setTextSize()重复。
方法要求参数以缩放像素为单位(
sp
),但你传递的是直像素。将
setTextSize()
调用更改为
setTextSize(TypedValue.COMPLEX\u UNIT\u PX,size)
。这应该是comment.ok..但如果答案正确,我不知道为什么人们会投反对票。我不是反对票,但你的答案基本上是否定的“他们之所以不同,是因为他们是不同的东西”。这在技术上可能是正确的,但实际上对OP没有帮助。