简体   繁体   English

将自定义样式设置为android中的自定义视图内的textviews

[英]Set custom style to textviews inside a custom view in android

I have a custom view class DoubleTextView.java which looks like below, 我有一个自定义视图类DoubleTextView.java,如下所示,

public class DoubleTextView extends LinearLayout {
    LinearLayout layout;
    TextView upTextView;
    TextView downTextView;
    Context mContext;

    public DoubleTextView(Context context) {

        super(context);
        mContext = context;
    }

    public DoubleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;


        String service = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li = (LayoutInflater) getContext().getSystemService(service);

        layout = (LinearLayout) li.inflate(R.layout.custom_double_text, this, true);

        upTextView = layout.findViewById(R.id.text_up);
        downTextView = layout.findViewById(R.id.text_down);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DoubleTextView);

        String upText = a.getString(R.styleable.DoubleTextView_upText);
        String downText = a.getString(R.styleable.DoubleTextView_downText);
        int upTextStyle = a.getInteger(R.styleable.DoubleTextView_styleUpText,R.style.ListOtherLightInfo);
        int downTextStyle = a.getInteger(R.styleable.DoubleTextView_styleDownText,R.style.ListOtherLightInfo);

        upText = upText == null ? "" : upText;
        downText = downText == null ? "" : downText;

        upTextView.setText(upText);
        downTextView.setText(downText);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            upTextView.setTextAppearance(R.style.ListOtherLightInfo);
            downTextView.setTextAppearance(downTextStyle);
        }
        a.recycle();
    }

    public DoubleTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }
}

Attributes to be set in DoubleTextView.class is below, 在DoubleTextView.class中设置的属性如下,

<declare-styleable name="DoubleTextView">
        <attr name="upText" format="string"/>
        <attr name="downText" format="string"/>
        <attr name="styleUpText" format="reference"/>
        <attr name="styleDownText" format="integer"/>

    </declare-styleable>

I am unable to add custom styles to the two textviews present in my DoubleTextView.java class. 我无法将自定义样式添加到DoubleTextView.java类中存在的两个textview中。 Custom styles are present in my styles.xml 自定义样式存在于我的styles.xml中

This is how I am trying to use this, 这就是我要使用的方式

<com.loconav.common.widget.DoubleTextView
                                app:upText="heklow"
                                app:downText="askjdnakjsndjasndjkasndasndj"
                                app:styleUpText="@style/BoldTextBlue"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"/>

But it is throwing error when app:styleUpText is used in xml , 但是在xml中使用app:styleUpText时,它会引发错误,

Method threw 'java.lang.UnsupportedOperationException' exception. 方法抛出“ java.lang.UnsupportedOperationException”异常。

Thanks in advance 提前致谢

使用getResourceId代替getInteger作为样式属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM