我有一个 xml 文件,我想通过使用扩展 View 的类将其更改为组件。我怎样才能做到这一点 ??
代码:
public class custom extends View {
public custom(Context context) {
super(context);
}
public custom(Context context , AttributeSet attrs) {
super(context , attrs);
}
}
这是我的 xml 代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.apd.ecryptfolders.custom">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"/>
谢谢!
您必须创建一个扩展布局根视图的类,然后膨胀 xml 布局
public class CustomView extends RelativeLayout {
public CustomView(Context context) {
super(context);
View view = inflate(getContext(), R.layout.layout_name, null);
addView(view);
// init layout
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
View view = inflate(getContext(), R.layout.layout_name, null);
addView(view);
// here you can apply custom attributes
// init layout
}
}
这些链接可以帮助您:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句