android 算定义布局xml

简介: 引用:http://abc20899.iteye.com/blog/1396565                     注意到其中的两处: xmlns:launcher=”http://schemas.android.com/apk/res/com.android.launcher” 和 launcher:defaultScreen="2" 可以看出在这个布局文件中,使用了自定义属性。

引用:http://abc20899.iteye.com/blog/1396565

<com.android.launcher2.DragLayer 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher" 
  
    android:id="@+id/drag_layer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
  
    <include layout="@layout/all_apps" /> 
  
    <!-- The workspace contains 3 screens of cells --> 
    <com.android.launcher2.Workspace 
        android:id="@+id/workspace" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:scrollbars="horizontal" 
        android:fadeScrollbars="true" 
        launcher:defaultScreen="2"> 
注意到其中的两处: 
xmlns:launcher=”http://schemas.android.com/apk/res/com.android.launcher” 
和 
launcher:defaultScreen="2" 
可以看出在这个布局文件中,使用了自定义属性。 

以前没遇到过,既然这里碰到了,就顺便学习下,下面就写个简单的示例,权当学习笔记,便于以后查阅。 
1. 定义一些自定义属性 
建立一个属性xml文件: values/attrs.xml, 内容如下: 

<?xml version="1.0" encoding="utf-8" ?> 
<resources> 
    <!-- the relation between the icon and text. --> 
    <attr name="relation"> 
        <enum name="icon_left"     value="0" /> 
        <enum name="icon_right"    value="1" /> 
        <enum name="icon_above"    value="2" /> 
        <enum name="icon_below"    value="3" /> 
    </attr> 
      
    <skip /> 
      
    <declare-styleable name="IconText"> 
         <attr name="relation" /> 
         <attr name="icon"         format="reference" /> 
         <attr name="text"         format="string" /> 
       <attr name="text_size"     format="dimension" /> 
       <attr name="text_color"    format="integer" /> 
       <attr name="space"         format="dimension" /> 
    </declare-styleable> 
      
</resources> 
解释如下: 
属性relation有4种可选值:icon_left, icon_right, icon_above,icon_below. 
属性icon的可选值为引用: 例如:"@/drawbable/icon". 
属性text的可选值为string, 例如: "Hello world", 也可是string的引用"@string/hello". 
属性text_size的可选值为尺寸大小,例如:20sp、18dip、20px等. 
属性text_color的可选值为整数,例如:"0xfffffff", 也可以是color的引用"@color/white". 

2. 定义一个能够处理这些属性值的view或者layout类 
package com.braincol.viewattrs; 
  
  
import android.content.Context; 
import android.content.res.TypedArray; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
  
public class IconTextView  extends LinearLayout { 
    private final static String TAG = "IconTextView"; 
    private final int ICON_LEFT = 0; 
    private final int ICON_RIGHT = 1; 
    private final int ICON_ABOVE = 2; 
    private final int ICON_BELOW = 3; 
  
    private TextView mTextView; 
    private ImageView mImageView; 
  
    private int mRelation = ICON_LEFT; 
    private String mText = ""; 
    private int mIconId; 
    private float mTextSize; 
    private int mSpace; 
  
    public IconTextView(Context context, AttributeSet attrs){ 
        super(context, attrs); 
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconText); 
  
        mRelation = a.getInt(R.styleable.IconText_relation, ICON_LEFT); 
        Log.d(TAG,"mRelation: "+mRelation); 
  
        mText = a.getString(R.styleable.IconText_text); 
        Log.d(TAG,"mText: "+mText); 
  
        mTextSize = a.getDimensionPixelSize(R.styleable.IconText_text_size, 12); 
        Log.d(TAG,"mTextSize: "+mTextSize); 
  
        mSpace = a.getDimensionPixelSize(R.styleable.IconText_space, 5); 
        Log.d(TAG,"mSpace: "+mSpace); 
  
        mIconId = a.getResourceId(R.styleable.IconText_icon, R.drawable.icon); 
        Log.d(TAG,"mIconId: "+mIconId); 
  
        a.recycle(); 
  
        mTextView = new TextView(context); 
        mTextView.setText(mText); 
        mTextView.setTextSize(mTextSize); 
  
        mImageView = new ImageView(context); 
        mImageView.setImageResource(mIconId);   
  
        int left    = 0; 
        int top     = 0; 
        int right    = 0; 
        int bottom    = 0; 
        int orientation = HORIZONTAL; 
        int textViewIndex = 0; 
        switch(mRelation){ 
        case ICON_ABOVE: 
            orientation = VERTICAL; 
            bottom = mSpace; 
            textViewIndex = 1; 
            break; 
        case ICON_BELOW: 
            orientation = VERTICAL; 
            top = mSpace; 
            break; 
        case ICON_LEFT: 
            right = mSpace; 
            textViewIndex = 1; 
            break; 
        case ICON_RIGHT: 
            left = mSpace; 
            break; 
        } 
        this.setOrientation(orientation); 
        this.addView(mImageView); 
        mImageView.setPadding(left, top, right, bottom); 
        this.addView(mTextView, textViewIndex); 
    } 

可以看出这个LinearLayout 子类IconTextView中只有两个元素,ImageView 和mTextView,通过从xml配置文件中读取属性值来决定icon和text的内容、相对位置及其它属性。 

3. 在layout布局文件中使用这个自定布局及其属性 
layout/main.xml: 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<com.braincol.viewattrs.IconTextView 
    android:id="@+id/icontext_01" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:icontext="http://schemas.android.com/apk/res/com.braincol.viewattrs" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    icontext:relation="icon_left" 
    icontext:icon="@drawable/hi" 
    icontext:text="hi,how are you!" 
    icontext:text_size="12sp" 
  /> 
    
</LinearLayout> 
可以看到我们在这个布局文件中加入了一个新的命名空间: 
xmlns:icontext="http://schemas.android.com/apk/res/com.braincol.viewattrs" 
并使用我们自定义的那些属性: 
icontext:relation="icon_left" 
icontext:icon="@drawable/hi" 
icontext:text="hi, how are you !" 
icontext:text_size="30sp" 
4. 在Activity中使用该布局 

package com.braincol.viewattrs; 
  
import android.app.Activity; 
import android.os.Bundle; 
  
public class ViewAttrs extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
}

相关文章
|
4月前
|
XML 编解码 搜索推荐
XML 布局小技巧
【10月更文挑战第24天】通过掌握这些 XML 布局小技巧,我们可以更轻松地设计出高质量的用户界面,提升用户体验。在实际应用中,要根据具体项目的需求和特点,灵活运用这些技巧,不断探索和创新,打造出独具特色的界面布局。
89 1
|
5月前
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
|
7月前
|
移动开发 监控 前端开发
构建高效Android应用:从优化布局到提升性能
【7月更文挑战第60天】在移动开发领域,一个流畅且响应迅速的应用程序是用户留存的关键。针对Android平台,开发者面临的挑战包括多样化的设备兼容性和性能优化。本文将深入探讨如何通过改进布局设计、内存管理和多线程处理来构建高效的Android应用。我们将剖析布局优化的细节,并讨论最新的Android性能提升策略,以帮助开发者创建更快速、更流畅的用户体验。
103 10
|
5月前
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
278 0
|
7月前
|
编解码 Android开发
【Android Studio】使用UI工具绘制,ConstraintLayout 限制性布局,快速上手
本文介绍了Android Studio中使用ConstraintLayout布局的方法,通过创建布局文件、设置控件约束等步骤,快速上手UI设计,并提供了一个TV Launcher界面布局的绘制示例。
148 1
|
7月前
|
存储 Java 数据库
基于全志H713 Android 11:给TvSettings添加default.xml默认值
本文介绍了在全志H713 Android 11平台上为TvSettings应用添加HDMI CEC功能的默认设置值的方法,通过修改SettingsProvider的源码和配置文件来实现默认值的设置,并提供了详细的步骤和测试结果。
206 0
基于全志H713 Android 11:给TvSettings添加default.xml默认值
|
7月前
|
XML Android开发 UED
"掌握安卓开发新境界:深度解析AndroidManifest.xml中的Intent-filter配置,让你的App轻松响应scheme_url,开启无限交互可能!"
【8月更文挑战第2天】在安卓开发中,scheme_url 通过在`AndroidManifest.xml`中配置`Intent-filter`,使应用能响应特定URL启动或执行操作。基本配置下,应用可通过定义特定URL模式的`Intent-filter`响应相应链接。
158 12
|
7月前
|
存储 Android开发 开发者
Android项目架构设计问题之定义RecyclerView的ViewHolder如何解决
Android项目架构设计问题之定义RecyclerView的ViewHolder如何解决
72 0
|
7月前
|
Android开发
Android项目架构设计问题之定义一个关闭当前页面的Action如何解决
Android项目架构设计问题之定义一个关闭当前页面的Action如何解决
36 0
|
7月前
|
JSON Android开发 数据格式
Android c++ core guideline checker 应用问题之JSON compilation database的定义如何解决
Android c++ core guideline checker 应用问题之JSON compilation database的定义如何解决

热门文章

最新文章

  • 1
    Android历史版本与APK文件结构
  • 2
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 3
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 4
    【03】微信支付商户申请下户到配置完整流程-微信开放平台创建APP应用-填写上传基础资料-生成安卓证书-获取Apk签名-申请+配置完整流程-优雅草卓伊凡
  • 5
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 6
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 7
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 8
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
  • 9
    Android实战经验之Kotlin中快速实现MVI架构
  • 10
    即时通讯安全篇(一):正确地理解和使用Android端加密算法