关于R.styleable的问题

本文介绍了解决Android开发中遇到的样式表被误删问题的方法。通过自定义styleable文件并调整ImageAdapter构造函数,成功实现了对galleryItemBackground资源ID的获取。

原来想直接想调用程序的东西,但是使用R.styleable的时候 eclipse不能解析了,后来发现原来被删除了此方法

public ImageAdapter(Context c) {
    mContext = c;
    TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
    mGalleryItemBackground = a.getResourceId(
            android.R.styleable.Theme_galleryItemBackground, 0);
    a.recycle();
}

 这种方法也就不能用了

然后

public ImageAdapter(Context c) {
    mContext = c;
    TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
    mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
    a.recycle();
}

唯一要做的就是自己去做一个styleable

res\values 

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="Gallery1"> 
        <attr name="android:galleryItemBackground" /> 
    </declare-styleable> 
</resources>
val ta = getContext().obtainStyledAttributes(attrs, R.styleable.TPWaveBallProgressView, defStyleAttr, defStyleRes) _progress = ta.getFloat(R.styleable.TPWaveBallProgressIndicator_waveProgress, 0f) _maxProgress = ta.getInteger(R.styleable.TPWaveBallProgressIndicator_waveMaxProgress, 100) wave1ColorStart = ta.getColor(R.styleable.TPWaveBallProgressIndicator_wave1ColorStart, DEFAULT_COLOR_START) wave1ColorEnd = ta.getColor(R.styleable.TPWaveBallProgressIndicator_wave1ColorEnd, DEFAULT_COLOR_END) wave1Cycle = ta.getDimensionPixelOffset(R.styleable.TPWaveBallProgressIndicator_wave1Cycle, DEFAULT_CYCLE) wave1Amplitude = ta.getDimensionPixelOffset(R.styleable.TPWaveBallProgressIndicator_wave1Amplitude, DEFAULT_AMPLITUDE) wave1Direction = ta.getInt(R.styleable.TPWaveBallProgressIndicator_wave1Direction, REVERSE) wave2ColorStart = ta.getColor(R.styleable.TPWaveBallProgressIndicator_wave2ColorStart, DEFAULT_COLOR_START) wave2ColorEnd = ta.getColor(R.styleable.TPWaveBallProgressIndicator_wave2ColorEnd, DEFAULT_COLOR_END) wave2Cycle = ta.getDimensionPixelOffset(R.styleable.TPWaveBallProgressIndicator_wave2Cycle, DEFAULT_CYCLE) wave2Amplitude = ta.getDimensionPixelOffset(R.styleable.TPWaveBallProgressIndicator_wave2Amplitude, DEFAULT_AMPLITUDE) wave2Direction = ta.getInt(R.styleable.TPWaveBallProgressIndicator_wave2Direction, FORWARD) outerCircleColor = ta.getColor(R.styleable.TPWaveBallProgressIndicator_outerCircleColor, Color.WHITE) ballRadius = ta.getDimensionPixelOffset(R.styleable.TPWaveBallProgressIndicator_ballRadius, 0) ta.recycle() }这个赋值不是只使用了后面的值 为什么要前一个参数呢
09-11
open class TPWaveBallProgressIndicator @JvmOverloads constructor( private var context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = R.style.Widget_TPDesign_WaveBallProgressIndicator ) : ConstraintLayout( context.wrapHighColorContrast(attrs, defStyleAttr, defStyleRes), attrs, defStyleAttr, defStyleRes ), TPWaveBallProgressView.OnProgressChangeListener { val progressView: TPWaveBallProgressView val progressTextView: TextView var onProgressListener: OnProgressListener? = null init { //参考实现:com.google.android.material.appbar.MaterialToolbar.MaterialToolbar(android.content.Context, android.util.AttributeSet, int) // Ensure we are using the correctly themed context rather than the context that was passed in. // 避免materialThemeOverlay属性覆盖的配置失效 context = getContext() val rootView = View.inflate(getContext(), R.layout.tpds_indicator_wave_ball_progress, this) progressView = rootView.findViewById(R.id.progress_wave_ball) progressTextView = rootView.findViewById(R.id.progress_tv) progressView.setOnProgressChangeListener(this) val ta = getContext().obtainStyledAttributes( attrs, R.styleable.TPWaveBallProgressIndicator, defStyleAttr, defStyleRes ) if (ta.hasValue(R.styleable.TPWaveBallProgressIndicator_waveProgressSize)) { val size = ta.getDimensionPixelSize(R.styleable.TPWaveBallProgressIndicator_waveProgressSize, -1) progressView.minimumWidth = size progressView.minimumHeight = size } progressView.progress = ta.getFloat(R.styleable.TPWaveBallProgressIndicator_waveProgress, 0f) progressView.maxProgress = ta.getInteger(R.styleable.TPWaveBallProgressIndicator_waveMaxProgress, 100) if (ta.hasValue(R.styleable.TPWaveBallProgressIndicator_wave1ColorStart)) { progressView.wave1ColorStart = ta.getColor(R.styleable.TPWaveBallProgressIndicator_wave1ColorStart, R.attr.colorPrimary) } if (ta.hasValue(R.styleable.TPWaveBallProgressIndicator_wave1ColorEnd)) { progressView.wave1ColorEnd = ta.getColor(R.styleable.TPWaveBallProgressIndicator_wave1ColorEnd, R.attr.colorPrimary) } if (ta.hasValue(R.styleable.TPWaveBallProgressIndicator_wave1Cycle)) { progressView.wave1Cycle = ta.getDimensionPixelOffset(R.styleable.TPWaveBallProgressIndicator_wave1Cycle, 0) } if (ta.hasValue(R.styleable.TPWaveBallProgressIndicator_wave1Amplitude)) { progressView.wave1Amplitude = ta.getDimensionPixelOffset(R.styleable.TPWaveBallProgressIndicator_wave1Amplitude, 0) } if (ta.hasValue(R.styleable.TPWaveBallProgressIndicator_wave1Direction)) { progressView.wave1Direction = ta.getInt(R.styleable.TPWaveBallProgressIndicator_wave1Direction, TPWaveBallProgressView.REVERSE) } if (ta.hasValue(R.styleable.TPWaveBallProgressIndicator_wave2ColorStart)) { progressView.wave2ColorStart = ta.getColor( R.styleable.TPWaveBallProgressIndicator_wave2ColorStart, R.attr.colorPrimary ) } if (ta.hasValue(R.styleable.TPWaveBallProgressIndicator_wave2ColorEnd)) { progressView.wave2ColorEnd = ta.getColor( R.styleable.TPWaveBallProgressIndicator_wave2ColorEnd, R.attr.colorPrimary ) } if (ta.hasValue(R.styleable.TPWaveBallProgressIndicator_wave2Cycle)) { progressView.wave2Cycle = ta.getDimensionPixelOffset(R.styleable.TPWaveBallProgressIndicator_wave2Cycle, 0) } if (ta.hasValue(R.styleable.TPWaveBallProgressIndicator_wave2Amplitude)) { progressView.wave2Amplitude = ta.getDimensionPixelOffset(R.styleable.TPWaveBallProgressIndicator_wave2Amplitude, 0) } if (ta.hasValue(R.styleable.TPWaveBallProgressIndicator_wave2Direction)) { progressView.wave2Direction = ta.getInt(R.styleable.TPWaveBallProgressIndicator_wave2Direction, TPWaveBallProgressView.FORWARD) } progressView.outerCircleColor = ta.getColor(R.styleable.TPWaveBallProgressIndicator_outerCircleColor, Color.WHITE) progressView.ballRadius = ta.getDimensionPixelOffset(R.styleable.TPWaveBallProgressIndicator_ballRadius, 0) val textAppearanceId = ta.getResourceId(R.styleable.TPWaveBallProgressIndicator_android_textAppearance, 0) val textColorId = ta.getResourceId(R.styleable.TPWaveBallProgressIndicator_android_textColor, 0) if (textAppearanceId != 0) { TextViewCompat.setTextAppearance(progressTextView, textAppearanceId) } if (textColorId != 0) { progressTextView.setTextColor(AppCompatResources.getColorStateList(getContext(), textColorId)) } onProgressChange(progressView, progressView.progress) ta.recycle() } override fun onProgressChange(view: TPWaveBallProgressView, progress: Float) { progressTextView.text = String.format("%s%%", progress.toInt()) onProgressListener?.onProgress(progress) } interface OnProgressListener { fun onProgress(mProgress: Float) } }解释一下这段代码 并使用java写出
09-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值