在什么样的场景下会使用到自定义属性:
1,自定义 View
2,自定义组合控件(一个页面上相同控件很多,显示出来的内容又不一样)
如何定义自定义属性:
1,找到 attrs.xml (res/values/attrs.xml)如果该文件没有可以自行创建
2,点击打开 attrs.xml 文件,进行自定义属性的定义
<resources>
<!---申明定义的属性的类型--->
<attr name="subhead" format="string" localization="suggested" />
<attr name="inputWhetherNumber" format="boolean" localization="suggested" />
<declare-styleable name="myText">
<!---定义需要用到的属性名--->
<attr name="subhead" />
<attr name="inputWhetherNumber" />
<attr name="view_width" format="dimension" />
</declare-styleable>
</resources>
通过以上的方式,自定义属性就定义好了,如何使用?
使用步骤(以下的使用都是通过自定义组合控件来完成):
1,找到我们需要使用该自定义组合控件的布局文件。
2,在该布局文件中找到该控件,需要注意该布局文件的命名空间的使用需要使用app
xmlns:app="http://schemas.android.com/apk/res-auto"
<com.jianzhi.tally.view.activity.function.customview.NormalItemLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/chuan_ming_text"
app:subhead="自定义属性使用,需要使用 app:来关联" />
3,app:subhead 该属性就是我们自定义的属性,值就是我们定义好的类型,我用的是String
4,通过以上的方式方法,我们将可以自定义属性和自定义组合控件相关联了。
那么我们定义好了值以后,如何在我们的自定义组合控件中获取到我们想要的值呢?
直接上代码,极其简单的代码。
// 这个就是我自己定义的一个组合控件,继承自相对布局
class NormalItemLayout(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {
private var mRootView: View
private var normalContent: TextView
init {
mRootView = LayoutInflater.from(context).inflate(R.layout.fragment_item_type_normal, this, true)
// 这个是获取到我们自定义属性的集合
val mTypeArray = context.obtainStyledAttributes(attrs, R.styleable.myText)
// 代码中想要获取到我们自定义的内容一定要怎么写才行。固定写法,这里只是获取了其中一个
val text = mTypeArray.getString(R.styleable.myText_android_subhead)
mTypeArray.recycle()
// 使用该属性的值
mRootView.findViewById<TextView>(R.id.detailItemTitle).text = text
}
}
以上通过,
1,如何定义自定义属性。
2,如何使用自定义属性。
3,如何获取自定义属性。
三个步骤,来完成自定义属性和自定义组合控件相结合。
自定义组合控件的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#11000000">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_45"
tools:ignore="UselessParent">
<TextView
android:id="@+id/detailItemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/dp_16"
android:text="@string/stop_car"
android:textColor="#1B2025"
android:textSize="@dimen/sp_16" />
<TextView
android:id="@+id/normalContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/detailItemTitle"
android:hint="@string/wait_input_value" />
<!-- <View-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="1dp"-->
<!-- android:layout_alignParentBottom="true"-->
<!-- android:layout_marginStart="@dimen/dp_8"-->
<!-- android:layout_marginEnd="@dimen/dp_8"-->
<!-- android:background="@drawable/xml_common_gray_line_bg" />-->
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
研究方向,自定义属性如何通过和 viewmodel 相关联,来完成绑定的操作,动态的改变值。
下期将会揭晓,该技术的实现方式和方法。
以上如有不对,大家指之,我并改之。一起进步