前言
在开发自定义view时,往往需要绑定xml布局文件,那具体怎么做呢?
自定义view和xml布局文件关联的思路是:在自定义view的构造函数中,通过LayoutInflater.from(context).inflate(R.layout.test_layout, this)关联xml布局文件。具体代码如下
1、代码
自定义布局——xml代码(test_layout.xml)
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
自定义组件——Java代码(TestView)
public class TestView extends LinearLayout {
public TestView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
/**
* 初始化组件
* @param context
*/
private void init(Context context) {
//
最后
以上就是清脆蜜粉最近收集整理的关于android自定义控件用xml赋值,Android自定义组件,并把自定义组件和自定义xml布局文件关联...的全部内容,更多相关android自定义控件用xml赋值,Android自定义组件,并把自定义组件和自定义xml布局文件关联内容请搜索靠谱客的其他文章。
发表评论 取消回复