Android 如何将视图添加到XML布局中
我有一个包含所有按钮和图像的XML布局,我希望在布局顶部有一个移动的云。所以我创建了一个视图并移动了我的云,但是我无法将视图与布局链接。这是我的查看代码Android 如何将视图添加到XML布局中,android,view,android-animation,Android,View,Android Animation,我有一个包含所有按钮和图像的XML布局,我希望在布局顶部有一个移动的云。所以我创建了一个视图并移动了我的云,但是我无法将视图与布局链接。这是我的查看代码 public class CloudView extends View { Bitmap cloud; int ChangingX; public CloudView(Context context) { // TODO Auto-generated constructor stub super(context);
public class CloudView extends View {
Bitmap cloud;
int ChangingX;
public CloudView(Context context) {
// TODO Auto-generated constructor stub
super(context);
cloud = BitmapFactory.decodeResource(getResources(), R.drawable.cloud);
ChangingX = 50;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(cloud, ChangingX , 50, null);
if (ChangingX < canvas.getWidth())
ChangingX += 2;
else
ChangingX = 50;
invalidate();
}
}
我是安卓动画新手,你能详细解释一下我如何将视图与布局链接起来吗。
如果它不起作用,除了视图之外,还有什么其他类可以使用
谢谢你的时间和考虑。对不起,我的英语不好。这是Android开发者链接,可能对你有用 如何定义属性: 要定义自定义属性,请向项目中添加资源。通常将这些资源放入
res/values/attrs.xml
文件中。下面是一个attrs.xml
文件的示例:
<resources>
<declare-styleable name="PieChart">
<attr name="showText" format="boolean" />
<attr name="labelPosition" format="enum">
<enum name="left" value="0"/>
<enum name="right" value="1"/>
</attr>
</declare-styleable>
</resources>
阅读以了解更多详细信息。请按照以下步骤操作:
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.activity_my_vault, null,false);
this.addContentView(viewToLoad);
使用布局充气器添加新视图。请在代码中显示如何使用充气器,我以前从未使用过它。请参见下面的答案。我需要在MainActivity或view activity中添加此项?!你的观点在哪里。您必须将此添加到主活动中。它会给我PointerNullException。我的MainActivity扩展活动我不应该有另一个类来扩展视图来制作动画和导入onDraw。正如我提到的,我在这方面是新手,请耐心等待我,我是指CloudView,当我说我的视图活动可以更改要充气的布局的名称时。不要使用相同的名称。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
<com.example.customviews.charting.PieChart
custom:showText="true"
custom:labelPosition="left" />
</LinearLayout>
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.activity_my_vault, null,false);
this.addContentView(viewToLoad);