怎么在view imageview上添加label一个view

zz_yun 的BLOG
用户名:zz_yun
文章数:565
评论数:158
访问量:2014484
注册日期:
阅读量:5863
阅读量:12276
阅读量:321812
阅读量:1032437
51CTO推荐博文
&iOS sdk中的view是UIView,我们可以很方便的自定义一个View。
创建一个 Window-based Application程序,在其中添加一个Hypnosister的类,这个类选择继承UIObject。修改这个类,使他继承:UIView
@interface HypnosisView :&UIView
自定义View的关键是定义drawRect: 方法,因为主要是通过重载这个方法,来改变view的外观。例如,可以使用下面代码绘制一个很多环中环的效果的view。
650) this.width=650;" id="code_img_opened_3fb7a9aa-cb9a-3dea091583" class="code_img_opened" src="/OutliningIndicators/ExpandedBlockStart.gif" style="margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 padding-top: 0 padding-right: 5 padding-bottom: 0 padding-left: 0 border-top-width: 0 border-right-width: 0 border-bottom-width: 0 border-left-width: 0 border-style: border-color: border-style: border-color: vertical-align: " alt="" />View Code
- (void)drawRect:(CGRect)rect {
// What rectangle am I filling?
CGRect bounds = [self bounds];
// Where is its center?
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;
// From the center how far out to a corner?
float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
// Get the context being drawn upon
CGContextRef context = UIGraphicsGetCurrentContext();
// All lines will be drawn 10 points wide
CGContextSetLineWidth(context, 10);
// Set the stroke color to light gray
[[UIColor lightGrayColor] setStroke];
// Draw concentric circles from the outside in
for (float currentRadius = maxR currentRadius & 0; currentRadius -= 20)
CGContextAddArc(context, center.x, center.y,
currentRadius, 0.0, M_PI * 2.0, YES);
CGContextStrokePath(context);
这样view的效果如下图:
650) this.width=650;" alt="" src="/images//1519.png" style="margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 border-top-width: 0 border-right-width: 0 border-bottom-width: 0 border-left-width: 0 border-style: border-color: border-style: border-color: " />
我们可以继续绘制一些东西,比如绘制文字,将下面代码添加带这个方法后面。
// Create a string
NSString *text = @&我是朱祁林,不是朱麒麟&;
// Get a font to draw it in
UIFont *font = [UIFont boldSystemFontOfSize:28];
// Where am I going to draw it?
CGRect textR
textRect.size = [text sizeWithFont:font];
textRect.origin.x = center.x - textRect.size.width / 2.0;
textRect.origin.y = center.y - textRect.size.height / 2.0;
// Set the fill color of the current context to black
[[UIColor blackColor] setFill];
// Set the shadow to be offset 4 points right, 3 points down,
// dark gray and with a blur radius of 2 points
CGSize offset = CGSizeMake(4, 3);
CGColorRef color = [[UIColor darkGrayColor] CGColor];
CGContextSetShadowWithColor(context, offset, 2.0, color);
// Draw the string
[text drawInRect:textRect
withFont:font];
650) this.width=650;" alt="" src="/images//3030.png" style="margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 border-top-width: 0 border-right-width: 0 border-bottom-width: 0 border-left-width: 0 border-style: border-color: border-style: border-color: " />
如果view过大,我们可以把它放置到一个UIScrollView中间,这样就可以进行拖动了。UIScrollView与View的关系如下图:
650) this.width=650;" alt="" src="/images//4248.png" style="margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 border-top-width: 0 border-right-width: 0 border-bottom-width: 0 border-left-width: 0 border-style: border-color: border-style: border-color: " />
使用下面代码创建一个比iPhone屏幕大4倍的View,然后通过UIScrollView来展示,代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//创建一个窗体大小的CGRect
CGRect wholeWindow = [[self window] bounds];
// 创建一个窗体大小的HypnosisView实例
view = [[HypnosisView alloc] initWithFrame:wholeWindow];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];
[[self window] addSubview:scrollView];
// Make your view twice as large as the window
CGRect reallyBigR
reallyBigRect.origin = CGPointZ
reallyBigRect.size.width = wholeWindow.size.width * 2.0;
reallyBigRect.size.height = wholeWindow.size.height * 2.0;
[scrollView setContentSize:reallyBigRect.size];
offset.x = wholeWindow.size.width * 0.5;
offset.y = wholeWindow.size.height * 0.5;
[scrollView setContentOffset:offset];
// Create the view
view = [[HypnosisView alloc] initWithFrame:reallyBigRect];
[view setBackgroundColor:[UIColor clearColor]];
[scrollView addSubview:view];
[scrollView release];
[[UIApplication sharedApplication] setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationFade];
[[self window] makeKeyAndVisible];
return YES;
这样我们就可以拖动来展示看不到的view了,如下图:
650) this.width=650;" src="/images//3378.png" alt="" style="margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 border-top-width: 0 border-right-width: 0 border-bottom-width: 0 border-left-width: 0 border-style: border-color: border-style: border-color: " />
通过UIScrollView我们还可以设置view的缩放功能,将下面代码添加到中。这样我们就可以使用两根手指缩放view了。
// Enable zooming
[scrollView setMinimumZoomScale:0.5];
[scrollView setMaximumZoomScale:5];
[scrollView setDelegate:self];
总结:文本简单的总结了一下自定义view的使用。
了这篇文章
类别:┆阅读(0)┆评论(0)ASP.NET MVC4 入门之添加一个View
ASP.NET MVC4 入门之添加一个View
在这一小节,我们将会修改类HelloWorldController来使用视图模板文件,把为客户端生成HTML响应的过程封装起来。我们将使用在ASP.NET MVC 3中介绍的Razor视图引擎来创建视图模板文件。基于Razor的视图模板使用.cshtml做文件扩展名,它提供了一种优雅的方式来使用c#生成HTML内容。在创建一个视图模板时Razor可以使我们编写更少的代码,使我们的编码过程快速流畅。目前Index方法只会返回硬编码在controller中的字符串。通过下面的代码可以使Index方法返回一个视图对象。public ActionResult Index()
return View();
}上面的Index方法是用一个视图模板来生成返回给浏览器的HTML响应。Controller方法(又称action方法),比如Index,通常返回一个ActionResult对象或者从ActionResult派生的对象,而不是像string这样的简单类型。在这个项目里,我们为Index方法添加一个视图模板,在Index方法内部右键单击,选择"Add View"。Add View对话窗口会出现,我们使用默认设置,直接单击Add按钮。这样做会创建MvcMoview\View\HelloWorld文件夹并且会在该文件夹下创建Index.cshtml文件,你可以在解决方案资源管理器(Solution Explorer)中看到。下面是新创建的Index.cshtml中的内容在最后一行加入新代码,修改后的index.cshtml文件内容如下@{
ViewBag.Title = "Index";
&h2&Index&/h2&
&p&Hello from our View Template!&/p&让我们启动程序浏览HelloWorld controller(:/mu/),controller中的Index方法并不会做太多的工作,只是单纯执行return View()语句,来指明Index方法会使用一个视图模板来来渲染浏览器。因为我们并没有显示指出要使用的视图模板文件的名字,ASP.NET MVC 会默认使用\Views\HelloWorld文件夹下的Index.cshtml视图文件。下面的截图显示了被硬编码在视图中的字符串"Hello from our View Template"。看起来好多了。但是,注意在浏览器标题栏里展示的"Index-My ASP.NET M..."和在页面上方大大的超链接"your logo here"。在"your logo here"之后是注册和登录的超链接,然后是Home,About和Contact页面的超链接,让我们来做点事情改变这个页面。改变Views和布局页面首先,来改变页面顶部的标题"your logo here."。这是每一个页面都会有的文本。尽管在应用程序中的每一个页面它都会出现,事实上在项目中只有一个地方实现了它。查看解决方案资源管理器的/Views/Shared文件夹并且打开_Layout.cshtml文件。这个文件被叫做布局页面(layout page)并且作为其它页面共享的“外壳”。布局模板允许我们为网站在一个地方制定HTML容器布局然后将其应用到多个页面中。找到代码中的@RederBody()那一行。RenderBody是一个占位符,在所有特定的视图中会替换掉它。例如,如果你选择了About超链接,Views\Home\About.cshtml视图会被渲染到RenderBody方法所在的地方。改变布局模板中网页头部的内容(body标签下的&header&),把"your logo here"改成"MVC Movie"&div class="float-left"&
&p class="site-title"&@Html.ActionLink("Mvc Movie", "Index", "Home")&/p&
&/div&使用下面的标记替换掉title元素中的内容&title&@ViewBag.Title - Movie App&/title&运行程序,我们会看到现在页面头部展示的是"MVC Movie"。点击About超链接,你同样会看到头部的"MVC Movie"。我们只需要在布局模板中做一次刚改,就可以影响到所有的页面。现在,让我们来改变一下Index视图的标题。打开MvcMovie\VIews\HelloWorld\Index.cshtml文件。有两个地方需要做修改:第一,出现在浏览提标题中的文字;第二,二级标题(&h2&元素).我们把这两处改的有点不一样,这样就能看出来哪一处代码会影响到程序的哪一部分。@{
ViewBag.Title = "Movie List";
&h2&My Movie List&/h2&
&p&Hello from our View Template!&/p&为了指定展现的HTML标题,上面的代码为ViewBag(存在于Index.cshtml视图模板中)对象的Title属性赋了值。如果你回过头来看一下布局模板中的代码,你会发现我们之前修改的布局模板html中的&head&节点力的&title&元素内部使用了ViewBag的这个属性值。通过使用ViewBag,我们可以轻松地在布局文件和视图模板之间传递参数。启动应用程序浏览ttp:///zhuxian/。注意一下浏览器的标题,主标题和此标题已经改变了(如果你没有看到改变的话,可能是因为你看到了浏览器缓存的内容,按下ctrl+F5强制浏览器向服务器请求新页面)浏览器标题由我们在Index.cshtml视图模板中设置的Viewbag.Title和在布局文件中添加的" – Movie App"共同构造出来。同样要注意到Index.cshtml和_Layout.cshtml视图模板中的内容是怎样合并到一起生成一个单一的HTML响应发送到浏览器的。布局模板使我们修改应用中的多个页面变得非常容易。我们仅仅使用的一点点”数据“,是硬编码的"Hello from out View Template",现在我们的MVC程序有了V和C,但是还没有M,马上我们就会介绍如何创建一个数据库并且从中检索model数据。从Controller向View传递数据在我们使用数据库和讨论model之前,先谈谈关于从controller向view传递数据的话题。Controller类在响应传入的URL请求时被调用。一个controller是你写代码来处理传入的浏览器请求,从数据库中检索数据最终决定将什么样的响应发回浏览器的类。在controller生成HTML响应的时候可以使用视图模板(View Template)。Controller负责为视图模板提供请求的任何数据或者对象。最好的实践是:视图模板完全不关心业务逻辑或者并且从不和数据库直接交互。视图模板应该仅仅使用到controller传递给它的数据。坚持“关注点分离(separation of concerns)”原则有助于我们写出整洁,可测试和易维护的代码。目前,HelloWorldController中的Welcom方法接收name和numTimes参数并且直接将其输出到浏览器。我们现在使用视图模板替代简单的字符串。视图模板将负责动态生成响应,这就意味着我们需要从controller传递恰当的数据给它。我们可以通过在controller中把数据存放到视图模板也可以访问的ViewBag对象中来做到这一点。让我们回到HelloWorldController.cs文件,并且更改Welcom方法,来给ViewBag对象添加Message和NumTimes数据。ViewBag是一个动态类型(c#里的dynamic),也就是说我们可以给它添加任何属性,在我们给ViewBag添加属性之前它是没有任何属性的。会自动将url中查询字符串里的参数映射到方法参数。完整的HelloWorldController.cs文件内容如下:using System.W
using System.Web.M
namespace MvcMovie.Controllers
public class HelloWorldController : Controller
// GET: /HelloWorld/
public ActionResult Index()
return View();
// GET: /HelloWorld/Welcome/
public ActionResult Welcome(string name, int numTimes = 1)
ViewBag.Message = "Hello " +
ViewBag.NumTimes = numT
return View();
}现在包含了数据的ViewBag会自动传递给View。接下来,我们需要一个Welcom视图模板,我们需要先编译一下项目。选择“Build”菜单里的"Build MvcMovie"选项。在Welcom方法内部右键单击选择"Add View"。会弹出如下的Add View对话框单击Add,在Welcom.cshtml文件里的&h2&元素下面添加代码。创建一个循环来按照用户的要求输出"Hello",完整的Welcom.cshtml文件内容如下@{
ViewBag.Title = "Welcome";
&h2&Welcome&/h2&
@for(int i=0;i&ViewBag.NumTi++)
&li&@ViewBag.Message&/li&
&/ul&运行应用程序访问如下的URL:URL里携带的数据通过会传递给controller。controller将数据打包到ViewBag对象然后传递给View。View将数据展示为html。在上面的例子中,我们使用ViewBag对象来从controller传递数据给view。在接下来的文章里,我们会使用view model来传递数据。使用view model来传递数据比ViewBag对象更好,更多的内容可以参考。
发表评论:
TA的最新馆藏[转]&12598人阅读
Android开发(290)
android view(17)
View是所有控件的一个基类,无论是布局(Layout),还是控件(Widget)都是继承自View类。只不过layout是一个特殊的view,它里面创建一个view的数组可以包含其他的view而已。&
这一篇文章把所有的layout和widget都统称为view,那么android是如何创建一个view的呢?&
一。在代码中直接new出来。&
比如说你要创建一个TextView的实例,那么你可以这样写:&
Java代码&&
TextView&text&=&new&TextView(c);&&&&
二。把控件写在xml文件中然后通过LayoutInflater初始化一个view。&
注意:下面的内容不是顺序的看的而是交替的看的。否则可能弄迷糊。&
Java代码&&
LayoutInflater&inflater&=&(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);&&
View&view&=&inflater.inflate(R.layout.resourceid,&null);&&
这样也得到了一个view的实例,让我们一步一步来看,这个view是怎么new出来的。&
看类android.view.LayoutInflater&
Java代码&&
&&&public&View&inflate(int&resource,&ViewGroup&root)&{&&
&&&&&&&return&inflate(resource,&root,&root&!=&null);&&
&&public&View&inflate(int&resource,&ViewGroup&root,&boolean&attachToRoot)&{&&
&&&&&&&XmlResourceParser&parser&=&getContext().getResources().getLayout(resource);&&
&&&&&&&try&{&&
&&&&&&&&&&&return&inflate(parser,&root,&attachToRoot);&&
&&&&&&&}&finally&{&&
&&&&&&&&&&&parser.close();&&
&&&&&&&}&&
public&View&inflate(XmlPullParser&parser,&ViewGroup&root,&boolean&attachToRoot)&{&&
&&&&&&&synchronized&(mConstructorArgs)&{&&
&&&&&&&&&&&final&AttributeSet&attrs&=&Xml.asAttributeSet(parser);&&
&&&&&&&&&&&mConstructorArgs[0]&=&mC&&&&&&&&
&&&&&&&&&&&View&result&=&&&
&&&&&&&&&&&try&{&&
&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&int&&&
&&&&&&&&&&&&&&&while&((type&=&parser.next())&!=&XmlPullParser.START_TAG&&&&&
&&&&&&&&&&&&&&&&&&&&&&&type&!=&XmlPullParser.END_DOCUMENT)&{&&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&if&(type&!=&XmlPullParser.START_TAG)&{&&
&&&&&&&&&&&&&&&&&&&throw&new&InflateException(parser.getPositionDescription()&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&+&&:&No&start&tag&found!&);&&
&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&final&String&name&=&parser.getName();&&
&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&if&(TAG_MERGE.equals(name))&{&&
&&&&&&&&&&&&&&&&&&&if&(root&==&null&||&!attachToRoot)&{&&
&&&&&&&&&&&&&&&&&&&&&&&throw&new&InflateException(&&merge&/&&can&be&used&only&with&a&valid&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&+&&ViewGroup&root&and&attachToRoot=true&);&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&rInflate(parser,&root,&attrs);&&
&&&&&&&&&&&&&&&}&else&{&&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&View&temp&=&createViewFromTag(name,&attrs);&&
&&&&&&&&&&&&&&&&&&&ViewGroup.LayoutParams&params&=&null;&&
&&&&&&&&&&&&&&&&&&&if&(root&!=&null)&{&&
&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&params&=&root.generateLayoutParams(attrs);&&
&&&&&&&&&&&&&&&&&&&&&&&if&(!attachToRoot)&{&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&temp.setLayoutParams(params);&&
&&&&&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&rInflate(parser,&temp,&attrs);&&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&if&(root&!=&null&&&&attachToRoot)&{&&
&&&&&&&&&&&&&&&&&&&&&&&root.addView(temp,&params);&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&if&(root&==&null&||&!attachToRoot)&{&&
&&&&&&&&&&&&&&&&&&&&&&&result&=&&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&}&catch&(XmlPullParserException&e)&{&&
&&&&&&&&&&&&&&&InflateException&ex&=&new&InflateException(e.getMessage());&&
&&&&&&&&&&&&&&&ex.initCause(e);&&
&&&&&&&&&&&&&&&throw&&&
&&&&&&&&&&&}&catch&(IOException&e)&{&&
&&&&&&&&&&&&&&&InflateException&ex&=&new&InflateException(&&
&&&&&&&&&&&&&&&&&&&&&&&parser.getPositionDescription()&&
&&&&&&&&&&&&&&&&&&&&&&&+&&:&&&+&e.getMessage());&&
&&&&&&&&&&&&&&&ex.initCause(e);&&
&&&&&&&&&&&&&&&throw&&&
&&&&&&&&&&&}&&
&&&&&&&&&&&return&&&
&&&&&&&}&&
&&&private&void&rInflate(XmlPullParser&parser,&View&parent,&final&AttributeSet&attrs)&&
&&&&&&&&&&&throws&XmlPullParserException,&IOException&{&&
&&&&&&&final&int&depth&=&parser.getDepth();&&
&&&&&&&int&&&
&&&&&&&while&(((type&=&parser.next())&!=&XmlPullParser.END_TAG&||&&
&&&&&&&&&&&&&&&parser.getDepth()&&&depth)&&&&type&!=&XmlPullParser.END_DOCUMENT)&{&&
&&&&&&&&&&&if&(type&!=&XmlPullParser.START_TAG)&{&&
&&&&&&&&&&&&&&&continue;&&
&&&&&&&&&&&}&&
&&&&&&&&&&&final&String&name&=&parser.getName();&&
&&&&&&&&&&&&&
&&&&&&&&&&&if&(TAG_REQUEST_FOCUS.equals(name))&{&&
&&&&&&&&&&&&&&&parseRequestFocus(parser,&parent);&&
&&&&&&&&&&&}&else&if&(TAG_INCLUDE.equals(name))&{&&
&&&&&&&&&&&&&&&if&(parser.getDepth()&==&0)&{&&
&&&&&&&&&&&&&&&&&&&throw&new&InflateException(&&include&/&&cannot&be&the&root&element&);&&
&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&parseInclude(parser,&parent,&attrs);&&
&&&&&&&&&&&}&else&if&(TAG_MERGE.equals(name))&{&&
&&&&&&&&&&&&&&&throw&new&InflateException(&&merge&/&&must&be&the&root&element&);&&
&&&&&&&&&&&}&else&{&&&&&&&&&&&
&&&&&&&&&&&&&&&final&View&view&=&createViewFromTag(name,&attrs);&&
&&&&&&&&&&&&&&&final&ViewGroup&viewGroup&=&(ViewGroup)&&&
&&&&&&&&&&&&&&&final&ViewGroup.LayoutParams&params&=&viewGroup.generateLayoutParams(attrs);&&
&&&&&&&&&&&&&&&rInflate(parser,&view,&attrs);&&
&&&&&&&&&&&&&&&viewGroup.addView(view,&params);&&
&&&&&&&&&&&}&&
&&&&&&&}&&
&&&&&&&parent.onFinishInflate();&&
View&createViewFromTag(String&name,&AttributeSet&attrs)&{&&
&&&&&&&if&(name.equals(&view&))&{&&
&&&&&&&&&&&name&=&attrs.getAttributeValue(null,&&class&);&&
&&&&&&&}&&
&&&&&&&if&(DEBUG)&System.out.println(&********&Creating&view:&&&+&name);&&
&&&&&&&try&{&&
&&&&&&&&&&&View&view&=&(mFactory&==&null)&?&null&:&mFactory.onCreateView(name,&&
&&&&&&&&&&&&&&&&&&&mContext,&attrs);&&
&&&&&&&&&&&if&(view&==&null)&{&&
&&&&&&&&&&&&&&&if&(-1&==&name.indexOf('.'))&{&&&&&&&&&
&&&&&&&&&&&&&&&&&&&view&=&onCreateView(name,&attrs);&&
&&&&&&&&&&&&&&&}&else&{&&
&&&&&&&&&&&&&&&&&&&view&=&createView(name,&null,&attrs);&&
&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&}&&
&&&&&&&&&&&if&(DEBUG)&System.out.println(&Created&view&is:&&&+&view);&&
&&&&&&&&&&&return&&&
&&&&&&&}&catch&(InflateException&e)&{&&
&&&&&&&&&&&throw&e;&&
&&&&&&&}&catch&(ClassNotFoundException&e)&{&&
&&&&&&&&&&&InflateException&ie&=&new&InflateException(attrs.getPositionDescription()&&
&&&&&&&&&&&&&&&&&&&+&&:&Error&inflating&class&&&+&name);&&
&&&&&&&&&&&ie.initCause(e);&&
&&&&&&&&&&&throw&&&
&&&&&&&}&catch&(Exception&e)&{&&
&&&&&&&&&&&InflateException&ie&=&new&InflateException(attrs.getPositionDescription()&&
&&&&&&&&&&&&&&&&&&&+&&:&Error&inflating&class&&&+&name);&&
&&&&&&&&&&&ie.initCause(e);&&
&&&&&&&&&&&throw&&&
&&&&&&&}&&
&public&final&View&createView(String&name,&String&prefix,&AttributeSet&attrs)&&
&&&&&&&&&&&throws&ClassNotFoundException,&InflateException&{&&
&&&&&&&Constructor&constructor&=&sConstructorMap.get(name);&&&
&&&&&&&Class&clazz&=&null;&&
&&&&&&&try&{&&
&&&&&&&&&&&if&(constructor&==&null)&{&&
&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&clazz&=&mContext.getClassLoader().loadClass(&&
&&&&&&&&&&&&&&&&&&&&&&&prefix&!=&null&?&(prefix&+&name)&:&name);&&
&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&if&(mFilter&!=&null&&&&clazz&!=&null)&{&&
&&&&&&&&&&&&&&&&&&&boolean&allowed&=&mFilter.onLoadClass(clazz);&&
&&&&&&&&&&&&&&&&&&&if&(!allowed)&{&&
&&&&&&&&&&&&&&&&&&&&&&&failNotAllowed(name,&prefix,&attrs);&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&constructor&=&clazz.getConstructor(mConstructorSignature);&&
&&&&&&&&&&&&&&&sConstructorMap.put(name,&constructor);&&&&&&&&
&&&&&&&&&&&}&else&{&&
&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&if&(mFilter&!=&null)&{&&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&Boolean&allowedState&=&mFilterMap.get(name);&&
&&&&&&&&&&&&&&&&&&&if&(allowedState&==&null)&{&&
&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&clazz&=&mContext.getClassLoader().loadClass(&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&prefix&!=&null&?&(prefix&+&name)&:&name);&&
&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&boolean&allowed&=&clazz&!=&null&&&&mFilter.onLoadClass(clazz);&&
&&&&&&&&&&&&&&&&&&&&&&&mFilterMap.put(name,&allowed);&&
&&&&&&&&&&&&&&&&&&&&&&&if&(!allowed)&{&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&failNotAllowed(name,&prefix,&attrs);&&
&&&&&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&}&else&if&(allowedState.equals(Boolean.FALSE))&{&&
&&&&&&&&&&&&&&&&&&&&&&&failNotAllowed(name,&prefix,&attrs);&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&}&&
&&&&&&&&&&&Object[]&args&=&mConstructorA&&
&&&&&&&&&&&args[1]&=&&&&&&&&
&&&&&&&&&&&return&(View)&constructor.newInstance(args);&&&&&&&
&&&&&&&}&catch&(NoSuchMethodException&e)&{&&
&&&&&&&&&&&InflateException&ie&=&new&InflateException(attrs.getPositionDescription()&&
&&&&&&&&&&&&&&&&&&&+&&:&Error&inflating&class&&&&
&&&&&&&&&&&&&&&&&&&+&(prefix&!=&null&?&(prefix&+&name)&:&name));&&
&&&&&&&&&&&ie.initCause(e);&&
&&&&&&&&&&&throw&&&
&&&&&&&}&catch&(ClassNotFoundException&e)&{&&
&&&&&&&&&&&&&
&&&&&&&&&&&throw&e;&&
&&&&&&&}&catch&(Exception&e)&{&&
&&&&&&&&&&&InflateException&ie&=&new&InflateException(attrs.getPositionDescription()&&
&&&&&&&&&&&&&&&&&&&+&&:&Error&inflating&class&&&&
&&&&&&&&&&&&&&&&&&&+&(clazz&==&null&?&&&unknown&&&:&clazz.getName()));&&
&&&&&&&&&&&ie.initCause(e);&&
&&&&&&&&&&&throw&&&
&&&&&&&}&&
在类android.content.res.Resources类中获取XmlResourceParser对象;&
Java代码&&
&public&XmlResourceParser&getLayout(int&id)&throws&NotFoundException&{&&
&&&&&return&loadXmlResourceParser(id,&&layout&);&&
ackage*/&XmlResourceParser&loadXmlResourceParser(int&id,&String&type)&&
&&&&&&&&&throws&NotFoundException&{&&
&&&&&synchronized&(mTmpValue)&{&&
&&&&&&&&&TypedValue&value&=&mTmpV&&
&&&&&&&&&getValue(id,&value,&true);&&
&&&&&&&&&if&(value.type&==&TypedValue.TYPE_STRING)&{&&
&&&&&&&&&&&&&return&loadXmlResourceParser(value.string.toString(),&id,&&
&&&&&&&&&&&&&&&&&&&&&value.assetCookie,&type);&&
&&&&&&&&&}&&
&&&&&&&&&throw&new&NotFoundException(&&
&&&&&&&&&&&&&&&&&&Resource&ID&#0x&&+&Integer.toHexString(id)&+&&&type&#0x&&&
&&&&&&&&&&&&&&&&&+&Integer.toHexString(value.type)&+&&&is&not&valid&);&&
&public&void&getValue(int&id,&TypedValue&outValue,&boolean&resolveRefs)&&
&&&&&&&&&throws&NotFoundException&{&&
&&&&&boolean&found&=&mAssets.getResourceValue(id,&outValue,&resolveRefs);&&
&&&&&if&(found)&{&&
&&&&&&&&&return;&&
&&&&&throw&new&NotFoundException(&Resource&ID&#0x&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&+&Integer.toHexString(id));&&
&&XmlResourceParser&loadXmlResourceParser(String&file,&int&id,&&
&&&&&&&&&int&assetCookie,&String&type)&throws&NotFoundException&{&&
&&&&&if&(id&!=&0)&{&&
&&&&&&&&&try&{&&
&&&&&&&&&&&&&synchronized&(mCachedXmlBlockIds)&{&&
&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&final&int&num&=&mCachedXmlBlockIds.&&
&&&&&&&&&&&&&&&&&for&(int&i=0;&i&&i++)&{&&
&&&&&&&&&&&&&&&&&&&&&if&(mCachedXmlBlockIds[i]&==&id)&{&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&return&mCachedXmlBlocks[i].newParser();&&
&&&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&XmlBlock&block&=&mAssets.openXmlBlockAsset(&&
&&&&&&&&&&&&&&&&&&&&&&&&&assetCookie,&file);&&
&&&&&&&&&&
&&&&&&&&&&
&&&&&&&&&&
&&&&&&&&&&&&&&&&&if&(block&!=&null)&{&&&&
&&&&&&&&&&&&&&&&&&&&&int&pos&=&mLastCachedXmlBlockIndex+1;&&
&&&&&&&&&&&&&&&&&&&&&if&(pos&&=&num)&pos&=&0;&&
&&&&&&&&&&&&&&&&&&&&&mLastCachedXmlBlockIndex&=&&&
&&&&&&&&&&&&&&&&&&&&&XmlBlock&oldBlock&=&mCachedXmlBlocks[pos];&&
&&&&&&&&&&&&&&&&&&&&&if&(oldBlock&!=&null)&{&&
&&&&&&&&&&&&&&&&&&&&&&&&&oldBlock.close();&&
&&&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&&&mCachedXmlBlockIds[pos]&=&&&
&&&&&&&&&&&&&&&&&&&&&mCachedXmlBlocks[pos]&=&&&
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&return&block.newParser();&&
&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&}&&
&&&&&&&&&}&catch&(Exception&e)&{&&
&&&&&&&&&&&&&NotFoundException&rnf&=&new&NotFoundException(&&
&&&&&&&&&&&&&&&&&&&&&&File&&&+&file&+&&&from&xml&type&&&+&type&+&&&resource&ID&#0x&&&
&&&&&&&&&&&&&&&&&&&&&+&Integer.toHexString(id));&&
&&&&&&&&&&&&&rnf.initCause(e);&&
&&&&&&&&&&&&&throw&&&
&&&&&&&&&}&&
&&&&&throw&new&NotFoundException(&&
&&&&&&&&&&&&&&File&&&+&file&+&&&from&xml&type&&&+&type&+&&&resource&ID&#0x&&&
&&&&&&&&&&&&&+&Integer.toHexString(id));&&
android.content.res.AssetManager类&
Java代码&&
&&&final&boolean&getResourceValue(int&ident,&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&TypedValue&outValue,&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&boolean&resolveRefs)&&
&&&&&&int&block&=&loadResourceValue(ident,&outValue,&resolveRefs);&&
&&&&&&if&(block&&=&0)&{&&
&&&&&&&&&&if&(outValue.type&!=&TypedValue.TYPE_STRING)&{&&
&&&&&&&&&&&&&&return&true;&&
&&&&&&&&&&}&&
&&&&&&&&&&outValue.string&=&mStringBlocks[block].get(outValue.data);&&
&&&&&&&&&&return&true;&&
&&&&&&return&false;&&
private&native&final&int&loadResourceValue(int&ident,&TypedValue&outValue,&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&boolean&resolve);&&
&&&&final&XmlBlock&openXmlBlockAsset(int&cookie,&String&fileName)&&
&&&&&&throws&IOException&{&&
&&&&&&synchronized&(this)&{&&
&&&&&&&&&&if&(!mOpen)&{&&
&&&&&&&&&&&&&&throw&new&RuntimeException(&Assetmanager&has&been&closed&);&&
&&&&&&&&&&}&&
&&&&&&&&&&int&xmlBlock&=&openXmlAssetNative(cookie,&fileName);&&
&&&&&&&&&&if&(xmlBlock&!=&0)&{&&
&&&&&&&&&&&&&&XmlBlock&res&=&new&XmlBlock(this,&xmlBlock);&&
&&&&&&&&&&&&&&incRefsLocked(res.hashCode());&&
&&&&&&&&&&&&&&return&&&
&&&&&&&&&&}&&
&&&&&&throw&new&FileNotFoundException(&Asset&XML&file:&&&+&fileName);&&
三 。通过view.findViewById(resourceid)获得一个view的实例&
android.View.View类中&
Java代码&&
&&&public&final&View&findViewById(int&id)&{&&
&&&&&&&if&(id&&&0)&{&&
&&&&&&&&&&&return&null;&&
&&&&&&&}&&
&&&&&&&return&findViewTraversal(id);&&
protected&View&findViewTraversal(int&id)&{&&
&&&&&&&if&(id&==&mID)&{&&
&&&&&&&&&&&return&this;&&
&&&&&&&}&&
&&&&&&&return&null;&&
android.View.ViewGroup类中&
Java代码&&
&protected&View&findViewTraversal(int&id)&{&&
&&&&&&&&if&(id&==&mID)&{&&
&&&&&&&&&&&&return&this;&&
&&&&&&&&}&&
&&&&&&&&final&View[]&where&=&mC&&
&&&&&&&&final&int&len&=&mChildrenC&&
&&&&&&&&for&(int&i&=&0;&i&&&&i++)&{&&
&&&&&&&&&&&&View&v&=&where[i];&&
&&&&&&&&&&&&if&((v.mPrivateFlags&&&IS_ROOT_NAMESPACE)&==&0)&{&&
&&&&&&&&&&&&&&&&v&=&v.findViewById(id);&&
&&&&&&&&&&&&&&&&if&(v&!=&null)&{&&
&&&&&&&&&&&&&&&&&&&&return&v;&&
&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&}&&
&&&&&&&&}&&
&&&&&&&&return&null;&&
四。通过activity的setContentView方法和findViewById获取一个view的实例。
getWindow().setContentView(layoutResID);设置window对象的view&
再来看看window对象是在哪里获得到的,在类Activity中找到&
mWindow = PolicyManager.makeNewWindow(this);&
它是由PolicyManager生成的。&
找到com.android.internal.policy.PolicyManager,找到方法&
Java代码&&
&&&public&static&Window&makeNewWindow(Context&context)&{&&
&&&&&&&return&sPolicy.makeNewWindow(context);&&
&&&private&static&final&String&POLICY_IMPL_CLASS_NAME&=&&
&&&&&&&&com.android.internal.policy.impl.Policy&;&&
&&&private&static&final&IPolicy&sP&&
&&&static&{&&
&&&&&&&try&{&&
&&&&&&&&&&&Class&policyClass&=&Class.forName(POLICY_IMPL_CLASS_NAME);&&
&&&&&&&&&&&sPolicy&=&(IPolicy)policyClass.newInstance();&&
&&&&&&&}&catch&(ClassNotFoundException&ex)&{&&
&&&&&&&&&&&throw&new&RuntimeException(&&
&&&&&&&&&&&&&&&&&&&POLICY_IMPL_CLASS_NAME&+&&&could&not&be&loaded&,&ex);&&
&&&&&&&}&catch&(InstantiationException&ex)&{&&
&&&&&&&&&&&throw&new&RuntimeException(&&
&&&&&&&&&&&&&&&&&&&POLICY_IMPL_CLASS_NAME&+&&&could&not&be&instantiated&,&ex);&&
&&&&&&&}&catch&(IllegalAccessException&ex)&{&&
&&&&&&&&&&&throw&new&RuntimeException(&&
&&&&&&&&&&&&&&&&&&&POLICY_IMPL_CLASS_NAME&+&&&could&not&be&instantiated&,&ex);&&
&&&&&&&}&&
找到com.android.internal.policy.impl.Policy类&
Java代码&&
public&PhoneWindow&makeNewWindow(Context&context)&{&&
&&&&return&new&PhoneWindow(context);&&
它其实是一个phoneWindow对象,继承自window对象&
找到com.android.internal.policy.impl.phoneWindow 看它内部是如何把resourceid加载成一个view的&
Java代码&&
private&ViewGroup&mContentP&&
&&&private&DecorView&mD&&
&&@Override&&
&&&public&void&setContentView(int&layoutResID)&{&&
&&&&&&&if&(mContentParent&==&null)&{&&
&&&&&&&&&&&installDecor();&&
&&&&&&&}&else&{&&
&&&&&&&&&&&mContentParent.removeAllViews();&&
&&&&&&&}&&
&&&&&&&mLayoutInflater.inflate(layoutResID,&mContentParent);&&
&&&&&&&final&Callback&cb&=&getCallback();&&
&&&&&&&if&(cb&!=&null)&{&&&&&&&&&&
&&&&&&&&&&&cb.onContentChanged();&&
&&&&&&&}&&
private&void&installDecor()&{&&
&&&&&&&if&(mDecor&==&null)&{&&
&&&&&&&&&&&mDecor&=&generateDecor();&&
&&&&&&&&&&&mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);&&
&&&&&&&&&&&mDecor.setIsRootNamespace(true);&&
&&&&&&&}&&
&&&&&&&if&(mContentParent&==&null)&{&&
&&&&&&&&&&&mContentParent&=&generateLayout(mDecor);&&
&&&&&&&&&&&mTitleView&=&(TextView)findViewById(com.android.internal.R.id.title);&&
&&&&&&&&&&&if&(mTitleView&!=&null)&{&&&&&&&&&&
&&&&&&&&&&&&&&&if&((getLocalFeatures()&&&(1&&&&FEATURE_NO_TITLE))&!=&0)&{&&
&&&&&&&&&&&&&&&&&&&View&titleContainer&=&findViewById(com.android.internal.R.id.title_container);&&
&&&&&&&&&&&&&&&&&&&if&(titleContainer&!=&null)&{&&
&&&&&&&&&&&&&&&&&&&&&&&titleContainer.setVisibility(View.GONE);&&
&&&&&&&&&&&&&&&&&&&}&else&{&&
&&&&&&&&&&&&&&&&&&&&&&&mTitleView.setVisibility(View.GONE);&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&if&(mContentParent&instanceof&FrameLayout)&{&&
&&&&&&&&&&&&&&&&&&&&&&&((FrameLayout)mContentParent).setForeground(null);&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&}&else&{&&
&&&&&&&&&&&&&&&&&&&mTitleView.setText(mTitle);&&
&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&}&&
&&&&&&&}&&
&protected&DecorView&generateDecor()&{&&
&&&&&&&return&new&DecorView(getContext(),&-1);&&
protected&ViewGroup&generateLayout(DecorView&decor)&{&&
&&&&&&&mDecor.startChanging();&&
&&&&&&&View&in&=&mLayoutInflater.inflate(layoutResource,&null);&&
&&&&&&&decor.addView(in,&new&ViewGroup.LayoutParams(MATCH_PARENT,&MATCH_PARENT));&&
&&&&&&&ViewGroup&contentParent&=&(ViewGroup)findViewById(ID_ANDROID_CONTENT);&&
&&&&&&&if&(contentParent&==&null)&{&&
&&&&&&&&&&&throw&new&RuntimeException(&Window&couldn't&find&content&container&view&);&&
&&&&&&&}&&
&&&&&&&return&contentP&&
private&final&class&DecorView&extends&FrameLayout&implements&RootViewSurfaceTaker&{&&
&&&&&&&&&public&DecorView(Context&context,&int&featureId)&{&&
&&&&&&&&&&&&super(context);&&
&&&&&&&&&&&&mFeatureId&=&featureId;&&
&&&&&&&}&&
@Override&&
&&&public&final&View&getDecorView()&{&&
&&&&&&&if&(mDecor&==&null)&{&&
&&&&&&&&&&&installDecor();&&
&&&&&&&}&&
&&&&&&&return&mD&&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1155368次
积分:11635
积分:11635
排名:第828名
原创:151篇
转载:262篇
评论:205条
(1)(12)(5)(1)(2)(1)(1)(6)(15)(3)(8)(6)(3)(27)(22)(25)(4)(6)(7)(7)(2)(2)(13)(8)(3)(2)(2)(11)(2)(3)(3)(14)(6)(7)(11)(1)(6)(9)(19)(14)(3)(7)(10)(3)(4)(7)(6)(14)(13)(4)(4)(22)(3)(6)(6)

我要回帖

更多关于 ios 导航栏上添加view 的文章

 

随机推荐