zoukankan      html  css  js  c++  java
  • android 自己定义标签的使用,实现扁平化UI设计

    2014年8月6日11:06:44

    android对自己定义标签的使用。实现扁平化UI设计:
    1.attrs.xml文件里自己定义标签
    如:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>


        <declare-styleable name="first">
    //reference指的是是从string.xml引用过来


            <attr name="name" format="reference" />
            <attr name="age">
    //flag是自定义的,类似于android:gravity="top"
                <flag name="child" value="10"></flag>
                <flag name="yong" value="18"></flag>
                <flag name="oldman" value="60"></flag>
            </attr>
    //dimension 指的是是从dimension.xml里引用过来的内容.
            <attr name="testSize" format="dimension"></attr>
        </declare-styleable>


    </resources>
    2.在布局文件里使用自己定义标签。对标签属性进行赋值
    如:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    //com.example.declarestyleabletest为清单文件里包的根路径    xmlns:test="http://schemas.android.com/apk/res/com.example.declarestyleabletest"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    //MyTest见以下
        <com.example.declarestyleabletest.MyTest
            android:id="@+id/tvTest"
            test:name="@string/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/test"
            test:age="oldman"
            test:testSize="@dimen/size" />


    </LinearLayout>
    3.MyTest.java:在类中能够得到布局文件里对属性赋的值,取到值之后能够对TextView的属性进行赋值改动样式
    如:
    package com.example.declarestyleabletest;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.util.AttributeSet;
    import android.widget.TextView;


    public class MyTest extends TextView {


    public MyTest(Context context, AttributeSet attrs) {
    super(context, attrs);


    TypedArray tArray = context.obtainStyledAttributes(attrs,
    R.styleable.first);
    String name = tArray.getString(R.styleable.first_name);
    int age = tArray.getInt(R.styleable.first_age, 100);
    //int dimen = tArray.getInt(R.styleable.first_testSize, 110);
    System.out.println("name:" + name + "age:" + age + "dimen:" );
    // 复用StyledAttributes
    tArray.recycle();

    }
    }
    4.在main.activity中通过id:tvTest使用改动过样式的view对象,如本例中的TextView。

    以下附上两个demo,第一个为简单的自己定义标签的赋值和取出,第二个为较为复杂的FlatUi扁平化Ui的实现。大家能够參考一下。

    扁平化效果图:


    http://download.csdn.net/detail/u014071669/7720493

  • 相关阅读:
    增强for循环
    java魔性的类型
    回溯算法
    UE4报错cmd.exe failed with args /c
    C#中GetHashCode的各类实现
    Unity3D中的meta文件笔记
    数值分析笔记(3)——数值计算中的原则
    数值分析笔记(2)——有效数字
    数值分析笔记(1)——误差的来源和分类
    数值分析笔记(0)——数值分析研究的对象和内容
  • 原文地址:https://www.cnblogs.com/llguanli/p/7039327.html
Copyright © 2011-2022 走看看