关于自定义布局,xml中配置属性(attrs)
16lz
2022-06-01
自定义布局中、在xml 自定义属性
例如:
这里app:? 属于xml命名空间,<com.momo.PollNumberProgress android:id="@+id/poll_progressbar1" android:layout_width="match_parent" android:layout_height="30dp" android:paddingLeft="10dp" android:visibility="gone" app:pollBackground="@color/poll_progbar1" />
在你的根Layout
xmlns:app="http://schemas.android.com/apk/res-auto"
这里在给NumberProgress 自定义一个Background颜色、 app:pollBackground=“@color/progbar1” ?
在看看Java代码如何取出来
public PollNumberProgress(Context context) { this(context, null);}public PollNumberProgress(Context context, AttributeSet attrs) { this(context, attrs, R.attr.numberProgressBarStyle);}public PollNumberProgress(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PollThemes, defStyleAttr, 0); pollBackground = a.getColor(R.styleable.PollThemes_pollBackground, context.getResources().getColor(R.color.clear)); a.recycle();}
引用attrs:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="PollThemes"> <attr name="numberProgressBarStyle" format="reference"/> <attr name="pollBackground" format="color"></attr> </declare-styleable></resources>类型:
关于Attrs用法:
参考:点击打开链接
参考:点击打开链接
更多相关文章
- android library projects cannot be launched
- Android(安卓)Studio安装后Gradle同步失败问题的有效解决方案
- android 打开文件
- Android(安卓)打开系统蓝牙设置
- android 打开软键盘 关闭软键盘
- Android(安卓)Proguard 如何混淆package name
- android 图表引擎AChartEngine(柱状图)
- Android打开关闭触摸提示音
- android studio 安装后无法启动问题