Android xmlns 的作用及其自定义实例详解

Android xmlns 的作用及其自定义实例详解,第1张

概述 Androidxmlns的作用及其自定义实例详解 xmlns:Android=\"http://schemas.android.com/apk/res/android的作用是:

 AndroID xmlns 的作用及其自定义实例详解

 xmlns:AndroID="http://schemas.androID.com/apk/res/androID的作用是:

这个是xml的命名空间,有了他,你就可以alt+/作为提示,提示你输入什么,不该输入什么,什么是对的,什么是错的,也可以理解为语法文件。或者语法判断器什么的

这个主要作用是在运行的时候那些控件的属性都是通过它来识别的,如果上面你写错了,不会有任何问题,但是在运行的时候就会有问题,提示你没有指定宽度等什么。这个是不用联网的。

AndroID 自定义的xmlns其实很简单,语法规则是:

在使用到自定义view的xml布局文件中需要加入xmlns:前缀=http://schemas.androID.com/apk/res/你的应用程序包路径.

下面是一个简单的例子:

结构图:

MyVIEw.java

package kexc.myVIEw;import androID.content.Context;import androID.content.res.TypedArray;import androID.util.AttributeSet;import androID.Widget.TextVIEw;public class MyVIEw extends TextVIEw {  private String mString = "Welcome to Kesion's blog";  public MyVIEw(Context context,AttributeSet attrs) { super(context,attrs); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyVIEw); int textcolor = a.getcolor(R.styleable.MyVIEw_textcolor,0XFFFFFFFF);   float textSize = a.getDimension(R.styleable.MyVIEw_textSize,36);   mString = a.getString(R.styleable.MyVIEw_Title); setText(mString); setTextSize(textSize); setTextcolor(textcolor); }}

 main.xml

<?xml version="1.0" enCoding="utf-8"?> <linearLayout  xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:test="http://schemas.androID.com/apk/res/kexc.myVIEw" androID:orIEntation="vertical"  androID:layout_wIDth="fill_parent"  androID:layout_height="fill_parent">  <TextVIEw   androID:layout_wIDth="fill_parent"   androID:layout_height="wrap_content"   androID:text="@string/hello"   />  <kexc.myVIEw.MyVIEw   androID:layout_wIDth="fill_parent"   androID:layout_height="fill_parent"  test:title="wo shi text"  test:textSize="20px"   test:textcolor="#fff"  /></linearLayout>

 属性文件 value/attrs.xml

<?xml version="1.0" enCoding="utf-8"?><resources> <declare-styleable name="MyVIEw">   <attr name="textcolor" format="color"/>  <attr name="textSize" format="dimension" />  <attr name="Title" format="string"/> </declare-styleable></resources>

运行结果:

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持! 总结

以上是内存溢出为你收集整理的Android xmlns 的作用及其自定义实例详解全部内容,希望文章能够帮你解决Android xmlns 的作用及其自定义实例详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146039.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存