`

Android xmlns 的作用及其自定义

阅读更多

虽然有点没看到但是留着以后会看的更明白的。

摘自:http://blog.csdn.net/chuchu521/article/details/8052855

 

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 的作用及其自定义实例详解  xmlns:Android=”http://schemas.android.com/apk/res/android的作用是: 这个是xml的命名空间,有了他,你就可以alt+/作为提示,提示你输入什么,不该输入什么,什么是对...

    Android自定义SeekBar滑动显示数字

    &lt;merge xmlns:android=http://schemas.android.com/apk/res/android xmlns:tools=http://schemas.android.com/tools&gt; &lt;RelativeLayout android:id=@+id/wrapper_seekbar_indicator android:la

    android自定义弹出框

    &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_...android自定义弹出框,很实用的

    Android Studio 创建自定义控件的方法

    我们知道,当系统控件并不能满足我们的需求时,我们就需要来创建自定义控件,主要有两种方法 (1)引入布局 下面来自定义一个控件,iPhone的...LinearLayout xmlns:android=http://schemas.android.com/apk/res/android

    Android代码-自定义圆形拖动条

    xmlns:swagpoints="http://schemas.android.com/apk/res-auto" android:id="@ id/seekbar_point" android:layout_width="match_parent" android:layout_height="340dp" android:layout_gravity="center" ...

    android自定义百分比布局

    xmlns:badboy="http://schemas.android.com/apk/res-auto" badboy自己随意写 2、使用自定义布局 //线型布局 android:layout_width="match_parent" android:layout_height="200dip" android:background="@...

    自定义CheckBox样式

    &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;!--这里是对应点击之后,CheckBox的背景图--&gt; &lt;item android:drawable="@drawable/checkBtn_on" android:state_checked="true" android:...

    Android自定义SeekBar实现视频播放进度条

    首先来看一下效果图,如下所示: 其中进度条如下: 接下来说一说我的思路,上面的进度拖动条有自定义的Thumb,在Thumb正上方有一...RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android andr

    Android 自定义View结合自定义TabLayout实现顶部标签滑动效果

    最近需要做一个app,需要用到tablayout实现顶部的滑动,用到了自定义item,不过没有用到tabitem,贴出来供大家学习,先看图吧,觉得满意的继续往下面看 具体代码实现: 我直接贴啦,能说的不多 主布局: fragment_...

    Android编程实现自定义控件的方法示例

    本文实例讲述了Android编程实现自定义控件的方法。分享给大家供大家参考,具体如下: 很多时候Android常用的控件不能满足我们的需求,那么我们就需要自定义一个控件了。今天做了一个自定义控件的实例,来分享下。 ...

    Android自定义View实现跟随手指移动的小兔子

    本文实例为大家分享了Android自定义View实现跟随手指移动的小兔子,供大家参考,具体内容如下 自定义的View实现跟随手指的小兔子 按前面的例子新创建一个project,再在project中新创建一个module 将需要的背景图和...

    Android自定义照相机的实例

    Android自定义照相机实现 近期小巫在学校有一个创新项目,也不是最近,是一个拖了很久的项目,之前一直没有去搞,最近因为要中期检查,搞得我跟小组成员一阵忙活,其实开发一款照相机软件并不太难,下面就是通过...

    android自定义imageview实现圆角图片

    本文实例为大家分享了android自定义imageview实现圆角图片的具体代码,供大家参考,具体内容如下 自定义图片的属性,对图片进行圆角切割 实现效果图: (1)在activity_main.xml文件布局,非常简单 &lt;?xml ...

    自定义SeekBar样式

    &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;!-- 按下状态 --&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/bg3" /&gt; &lt;!-- 普通无焦点状态 --&gt; &lt;item ...

    ratingbar 自定义星星效果

    &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:id="@+android:id/background" android:drawable="@drawable/xixi0" /&gt; &lt;item android:id="@+android:id/...

    Android编程实现自定义title功能示例

    本文实例讲述了Android编程实现自定义title功能。分享给大家供大家参考,具体如下: 这里我在前面加了个logo,而且改变了title的背景和高度。 首先编写title的布局文件,title.xml: &lt;?xml version=1.0 ...

    Android自定义照相机倒计时拍照

    自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件: 两个TextView是用来显示提示信息和倒计时的秒数的 相关教程:Android开发从相机或相册获取图片裁剪 Android启动相机拍照并返回图片 &lt;...

    Android编程使用自定义shape实现shadow阴影效果的方法

    本文实例讲述了Android编程使用自定义shape实现shadow阴影效果的方法。分享给大家供大家参考,具体如下: 直接上xml文件, 并且附上相应的解析: &lt;?xml version=1.0 encoding=utf-8?&gt; &lt;selector xmlns:android...

    Android使用xml自定义图片实例详解

    Android使用xml自定义图片实例详解 实现效果图: 白色圆角图片 bg_round_rectangle_white.xml &lt;?xml version=1.0 encoding=utf-8?&gt; &lt;shape xmlns:android=http://schemas.android.com/apk/res/android ...

Global site tag (gtag.js) - Google Analytics