【发布时间】:2018-10-29 18:44:35
【问题描述】:
如果您创建一个扩展父视图的自定义视图,您如何从父视图继承所有样式和属性?
例如,如果我在 xml 中声明一个 SeekBar 并为其设置样式,则一切正常。如果我在没有其他方法的情况下扩展此视图,则所有默认样式和格式都将消失。
原始 xml。这里的样式按预期工作:
<SeekBar
android:id="@+id/seekBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_default"
android:layout_marginEnd="@dimen/margin_default"
android:thumb="@drawable/ic_custom_thumb"/>
如果我在不添加任何内容的情况下扩展 SeekBar,则所有样式都不同。例如轨道颜色不同,拇指两边被切断。
class CustomSeekBar : SeekBar {
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)}
还有xml:
<com.namespace.CustomSeekBar
android:id="@+id/seekBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_default"
android:layout_marginEnd="@dimen/margin_default"
android:thumb="@drawable/ic_custom_thumb"/>
我需要做些什么才能使这个 CustomSeekBar 的外观和功能与默认的完全一样?尝试制作一个自定义视图,在不更改默认 xml 样式/主题等的情况下添加一些功能。我需要重建所有默认样式和主题,还是有办法继承它们?
【问题讨论】:
标签: android kotlin android-custom-view