如何将View添加到XML布局android
发布时间:2020-12-16 00:42:23 所属栏目:百科 来源:网络整理
导读:我有一个 XML布局,包含我的所有按钮和图像,我想在我的布局顶部移动云.所以我创建了一个视图并使我的云移动,但是我无法将视图与布局链接起来.这是我的观看代码 public class CloudView extends View {Bitmap cloud;int ChangingX;public CloudView(Context co
我有一个
XML布局,包含我的所有按钮和图像,我想在我的布局顶部移动云.所以我创建了一个视图并使我的云移动,但是我无法将视图与布局链接起来.这是我的观看代码
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(); } } 这是我的MainActivity public class MainActivity extends Activity { CloudView myView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); myView = new CloudView(this); setContentView(myView); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true; } } 我是动画片中的新手,你可以详细解释我如何将视图与布局相关联. 感谢您的时间和考虑.抱歉我的英语不好
这是Android Developer Link可能对我有用.
Define Custom Attributes 如何定义属性: 要定义自定义属性,请向项目添加资源.习惯上将这些资源放入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> 如何在XML中使用 <?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> 阅读更多详细信息请遵循. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |