Material Design Android Library使用步骤

比如Button:

Button.png

<code>
<com.gc.materialdesign.views.ButtonFlat
android:id="@+id/buttonflat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1E88E5"
android:text="Button" />
</code>

注意

  • 在使用有的控件的时候,会用到自定义属性,比如CheckBox,需要在xml的最外层添加命名空间,代码如下:
    <pre>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    </RelativeLayout>
    </pre>
  • 如果不添加命名空间会出现如下错误:unbound prefix
  • unbound prefix意思是前缀未被约定,比如我们写android:text="Iloveu",就引用了android空间中的text属性
  • 所以,如果再遇到unbound prefix的问题,请查看命名空间有没有定义完全,或者是某部分隶属的布局中所使用的命名空间是否被定义了。一般来说,在xml的最开始就应该把所有要用到的xmlns声明好.

一个实际的例子:

Login_activity.xml(一个登陆的界面):
code:
<pre>
<code>
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout

android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:orientation="vertical"
android:background="#eeeeee" >

<RelativeLayout
    android:id="@+id/login_top"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="@drawable/menu_list" >

    <ImageView
        android:id="@+id/login_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="10dp"
        android:src="@drawable/menu" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="20sp"
        android:text="@string/login" />

</RelativeLayout>

<LinearLayout
    android:id="@+id/login_bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/login_top"
       android:layout_marginStart="5dp"
       android:layout_marginEnd="5dp"
    android:orientation="vertical" >            
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="15dp"
        android:orientation="horizontal" >
        
        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@string/email_label"
            android:textSize="18sp"
            android:labelFor="@+id/email_address" />
            
        <EditText
            android:id="@+id/email_address"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:layout_weight="1" 
            android:hint="@string/email" />
        
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="15dp"
        android:orientation="horizontal" >
        
        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@string/pass_label"
            android:textSize="18sp"
            android:labelFor="@+id/pass_input" />
            
        <EditText
            android:id="@+id/pass_input"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPassword" />

    </LinearLayout>

    <TextView
        android:id="@+id/login_msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:text=""
        android:textSize="16sp"
        android:padding="10dp"
        android:visibility="gone" />        
        <LinearLayout
           
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"> 
            <RelativeLayout
               android:layout_width="wrap_content"
               android:layout_height="wrap_content">
        <com.gc.materialdesign.views.CheckBox
            
            android:layout_marginTop="20dp"
            android:id="@+id/acconut_checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:background="#1E88E5"
            materialdesign:check="true" />
        </RelativeLayout>
          <com.gc.materialdesign.views.ButtonFlat
           android:id="@+id/forgotpass_button"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"                
           android:layout_marginTop="20dp"
           android:layout_gravity="right"
           android:text="@string/forgot_pass"
           android:textSize="16sp" />
    </LinearLayout>
       <com.gc.materialdesign.views.ButtonRectangle
           android:id="@+id/login_button"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_marginTop="10dp"
           android:text="@string/login"
           android:textSize="20sp" />
       


       
       <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:text="@string/no_account"
        android:textSize="18sp" />
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="15dp"
        android:orientation="horizontal" >
        
        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@string/pass_repeat"
            android:textSize="18sp"
            android:labelFor="@+id/pass_repeat" />
            
        <EditText
            android:id="@+id/pass_repeat"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPassword"
            android:hint="@string/repeat_pass" />
    </LinearLayout>
    
</LinearLayout>

</LinearLayout>
</RelativeLayout>
</code>
</pre>

  • 提示:The following classes could not be instantiated:

  • com.gc.materialdesign.views.CheckBox (Open Class, Show Error Log)
    See the Error Log (Window > Show View) for more details.
    Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse

  • 界面如图:

Login.png
  • 疑惑:
    逻辑代码并无问题,第三方包也导入,但就是不能运行APP,基本排除了所有的问题,还是时不时出现问题,这个时候就要怀疑Eclipse自身的问题了。
  • 解决办法:
    重启Eclipse
    如果不行,重启电脑
  • 终于看见结果了:
Login
  • 个人心得:
    因为使用了第三方的Material Design Library 的库,我发现有时候该库中的R文件消失了,project就会出现问题,然后将build all 生产R文件,project无问题。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容