815
技術社區[雲棲]
Android自定義控件及按下效果
在應用程序開發中,經常要對android控件進行自定義來實現界麵的風格統一,對網上的資源進行搜索整理如下:
自定義EditText
一, 通過改變背景圖片來實現EditText的自定義
(1) 在drawable裏添加my_edittext.xml 代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="https://schemas.android.com/apk/res/android">
- <item android:state_focused="true" android:drawable="@drawable/edit1" />
- <item android:drawable="@drawable/edit" />
- </selector>
- 注:正常狀態時背景圖片為edit,而edit1為EditText為編輯狀態
(2) 在value文件下添加 my_edittext_style.xml代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="suretouch_edittext_style" parent="@android:style/Widget.EditText">
- //引入drawable下的my_edittext.xml文件
- <item name="android:background">@drawable/my_edittext</item>
- </style>
- </resources>
(3)在自己的布局文件中加入自定義樣式就完成了。
- <EditText
- android:id="@+id/admin_email"
- android:layout_width="461dip"
- android:layout_height="48dip"
- style="@style/my_edittext_style"
- />
使用上麵方式實現的搜索框,布局文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="260dip">
- <EditText
- android:id="@+id/findEditText"
- android:layout_alignParentTop="true"
- android:singleLine="true"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="請輸入查詢的類容"/>
- <ImageView
- android:id="@+id/findButton"
- android:src="@drawable/findbutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="4dip"
- android:layout_marginRight="2dip"
- android:adjustViewBounds="true"
- android:layout_alignTop="@id/findEditText"
- android:layout_alignRight="@id/findEditText"
- android:layout_alignBottom="@id/findEditText"/>
- </RelativeLayout>
drawable中的xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <selector xmlns:android="https://schemas.android.com/apk/res/android">
- <item android:state_pressed="true" android:drawable="@drawable/find0" />
- <item android:state_focused="true" android:drawable="@drawable/find1" />
- <item android:state_enabled="true" android:drawable="@drawable/find1" />
- </selector>
圖片
二,自定義EditText的邊框
(1)創建自定義MyEditText類
- package com.util;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.widget.EditText;
- public class MyEditText extends EditText {
- private Paint mPaint;
- private Context mContext;
- public MyEditText(Context context) {
- super(context);
- }
- // public MyEditText(Context context, AttributeSet attrs, int defStyle) {
- // super(context, attrs, defStyle);
- // // TODO Auto-generated constructor stub
- // }
- //
- public MyEditText(Context context, AttributeSet attrs) {
- super(context, attrs);
- mContext=context;
- // TODO Auto-generated constructor stub
- //定義畫筆
- mPaint = getPaint();
- //定義筆畫粗細樣式
- mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
- //定義筆畫顏色
- mPaint.setColor(Color.GRAY);
- }
- public void onDraw(Canvas canvas){
- super.onDraw(canvas);
- int w = getWidth();
- int h = getHeight();
- //下劃線
- // canvas.drawLine(0,h-1, w-1, h-1, mPaint);
- //下邊框
- canvas.drawLine(0, h, w, h, mPaint);
- //右邊框
- canvas.drawLine(w, 0, w, h, mPaint);
- //左邊框
- canvas.drawLine(0, 0, 0, h, mPaint);
- //上邊框
- canvas.drawLine(0, 0, w, 0, mPaint);
- }
- }
(2)在布局文件中直接引用自定義控件
- //注意這裏 如果不行<View class="com.util.MyEditText">
- <com.util.MyEditText
- android:id="@+id/register_password"
- android:layout_width="461dip"
- android:layout_height="48dip"
- android:layout_x="398dip"
- android:layout_y="353dip"
- android:password="true"
- style="?android:attr/textViewStyle"
- android:background="@null"
- android:textColor="@null"
- >
圖
三,按鈕按下效果,非常簡單同樣是使用selector
(1) 在drawable中的xml文件中,代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="https://schemas.android.com/apk/res/android">
- <item android:state_pressed="false"
- android:drawable="@drawable/register_up" />
- <item android:state_pressed="true"
- android:drawable="@drawable/register_down" />
- <item android:state_focused="true"
- android:drawable="@drawable/register_down" />
- <item android:drawable="@drawable/register_up" />
- </selector>
(2) ImageButton在xml布局文件中的代碼:
- <ImageButton
- android:id="@+id/register_btn"
- android:src="@drawable/suretouch_register_button"
- android:layout_width="158dip"
- android:layout_height="149dip"
- android:layout_x="893dip"
- android:layout_y="270dip"
- android:scaleType="centerCrop"
- android:background="@null"
- />
最後更新:2017-04-02 16:48:06
上一篇:
android:MotionEvent
下一篇:
Android自定義button的實現,未選中,按下,選中效果
ios ipa apple company 開發者賬號申請分享攻略
多條記錄求和問題
Speeding up Migration on ApsaraDB for Redis
3.3.4 PCI設備進行DMA寫時發生Cache命中
數據工程師的崛起
J2EE中HTTP method GET/Post is not supported by this URL
相關聊天工具 微(陸(台球熱賽娛樂賽每晚南口路底(水樹奈(閆(中華人民共和國稅收征收管理法(主席令第四十九號) 2015年8月15日 - 會關於修改〈中華人民共和國文物保護法〉等十二部法律的決定》(主席令第...第八十九條 納稅人、扣繳義務人可以委托稅務代
用shell腳本合並多個文件內容
java.util.concurrent包(4)——讀寫鎖ReentrantReadWriteLock
SQL COUNT(column_name) 語法