如何传递的AttributeSet作为参数 [英] How to pass AttributeSet as a parameter

查看:2609
本文介绍了如何传递的AttributeSet作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图落实P​​edram从<一个更新的解决方案href=\"http://stackoverflow.com/questions/21333866/create-a-circular-progressbar-in-android-which-rotates-on-circular-path\">this回答,但我不知道创建 CircleProgressBar 的新实例。它需要的AttributeSet作为参数传递,但如何得到它?

  CircleProgressBar circleProgressBar =新CircleProgressBar(MainActivity.this,?);


解决方案

在一个视图是通过XML定义布局虚增的AttributeSet 构造被使用。如果你在code构造一个,你应该使用单参数的构造函数(例如新CircleProgressBar(MainActivity.this))。如果没有定义单参数的构造函数,你只需要添加它。你只需要添加一些的getter / setter方法​​的属性,如果你希望能够从Java code内完全构造它。

另外,只是单个项目定义布局XML(例如名称view_progress_circle.xml):

 &LT; com.your.packagename.CircleProgressBar
    机器人:layout_width =100dp
    机器人:layout_height =100dp
    //等等。这里添加其他属性
    /&GT;

然后在code,与创建它:

  CircleProgressBar栏=(CircleProgressBar)LayoutInflater.from(MainActivity.this)
        .inflate(R.layout.view_progress_circle,父母,假的):

其中,的ViewGroup 你要附加视图。

I'm trying to implement updated solution by Pedram from this answer, but I don't know to create a new instance of CircleProgressBar. It requires AttributeSet to be passed as a parameter, but how to get it?

CircleProgressBar circleProgressBar = new CircleProgressBar(MainActivity.this, ?);

解决方案

The AttributeSet constructor is used when a view is inflated through a layout defined in XML. If you're constructing one in code, you should use the single argument constructor (e.g. new CircleProgressBar(MainActivity.this)). If the single-argument constructor is not defined, you just need to add it. You'll just need to add some getters/setters for the properties if you want to be able to construct it entirely from within Java code.

Alternately, just define a layout XML (example name view_progress_circle.xml) with a single item:

<com.your.packagename.CircleProgressBar
    android:layout_width="100dp"
    android:layout_height="100dp"
    // etc. add other attributes here
    />

Then in code, create it with:

CircleProgressBar bar = (CircleProgressBar) LayoutInflater.from(MainActivity.this)
        .inflate(R.layout.view_progress_circle, parent, false):

where parent is the ViewGroup you're going to attach the view to.

这篇关于如何传递的AttributeSet作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆