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无问题。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容