android 自定义view添加自定义xml属性
16lz
2021-01-25
第一步:在valuse下面新建xml文件 attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources>
<declare-styleable name="RangeSeekbar"> // 第二步:在valus下新建属性 <attr name="letMargin" format="integer" /> </declare-styleable>
</resources>
第三步:在xml定义 定义一个xmlns: http://schemas.android.com/apk/res/ +包名 设置percent:letMargin="20" <com.example.androidtest.activity.view.PerCentView xmlns:percent=" http://schemas.android.com/apk/res/com.example.androidtest" percent:letMargin="20"
android:id="@+id/percent"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
/>
第四部:在自定义view中调用 public PerCentView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RangeSeekbar); int left = typedArray.getInteger(R.styleable.RangeSeekbar_letMargin, 0); Log.e("tag", "dddd" + left); }
<declare-styleable name="RangeSeekbar"> // 第二步:在valus下新建属性 <attr name="letMargin" format="integer" /> </declare-styleable>
</resources>
第三步:在xml定义 定义一个xmlns: http://schemas.android.com/apk/res/ +包名 设置percent:letMargin="20" <com.example.androidtest.activity.view.PerCentView xmlns:percent=" http://schemas.android.com/apk/res/com.example.androidtest" percent:letMargin="20"
android:id="@+id/percent"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
/>
第四部:在自定义view中调用 public PerCentView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RangeSeekbar); int left = typedArray.getInteger(R.styleable.RangeSeekbar_letMargin, 0); Log.e("tag", "dddd" + left); }
更多相关文章
- android 设置布局横屏竖屏
- Android(安卓)实现一个Service应用
- 使用ImageSpan图标不截断的方法
- Android自定义属性时format选项可以取用的值
- Android(安卓)Intent初步试用
- Tutorials ---- Hello, World
- android的AndroidManifest.xml文件介绍
- 初学Android,图形图像之在指定点(坐标)播放动画(三十五)
- Android(安卓)自定义SeekBar 实现分段显示不同背景颜色