关于自定义布局,xml中配置属性(attrs)
16lz
2021-01-23
自定义布局中、在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用法:
参考:点击打开链接
参考:点击打开链接
更多相关文章
- ScrollView属性fillViewport解决android布局不能撑满全屏的问题
- Android ImageView的scaleType属性与adjustViewBounds属性
- 有关Android中EditText的一些属性
- Android自定义属性,format详解
- Android 错误 :TextView中属性ellipsize的 值为start、middle可能
- 优化布局在Android–减少过度渲染
- Android 自定义布局对话框避免全屏的设置
- Android RecyclerView网格布局的学习
- android中的一个属性动画,可以显示更多的一个案例