当前位置: 移动技术网 > IT编程>移动开发>Android > Android xmlns 的作用及其自定义实例详解

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

枪神纪刀锋去衣图,香港吉野门,黑人牙膏广告曲

 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>

运行结果:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网