1. 程式人生 > >android中自定義View設定屬性

android中自定義View設定屬性

記錄一下,以方便以後用到,再次出錯

1.首先需要在values/下面新建一個attrs.xml檔案,將一些需要的一些屬性填寫進去,比如:


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="customTitle">
     <attr name="titleText" format="string"/>   
     <attr name="titleColor" format="color"/>
     <attr name="titleSize" format="dimension"/>
    </declare-styleable> 
    </resources>

2.然後再佈局中的xml中,需要填寫名稱空間,


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


     <com.custom.CustomText01
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        custom:titleText="測試"
        custom:titleColor="#ffff00"
        custom:titleSize="48sp"
        /> 
</RelativeLayout>

3.需要在自定義View的第三個建構函式中,拿到相應的自定義屬性


public CustomText01(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.customTitle);
titleText=array.getString(R.styleable.customTitle_titleText);
color=array.getColor(R.styleable.customTitle_titleColor, 0Xff0000);
size=array.getDimensionPixelSize(R.styleable.customTitle_titleSize,20);

array.recycle();

}

4.重寫OnMeasure()方法(可重寫,可不重寫,視情況而定,有時候設定控制元件的寬高為wrap時,在螢幕上顯示時,會全屏顯示,這時候需要重寫)

5.OnDraw()方法裡面開始繪製.

相關推薦

android定義View設定屬性

記錄一下,以方便以後用到,再次出錯 1.首先需要在values/下面新建一個attrs.xml檔案,將一些需要的一些屬性填寫進去,比如: <?xml version="1.0" encoding="utf-8"?> <resources>    

Android定義View的研究 -- 在XML引用定義View

如果在一直使用SetContentView(new HellwView(this)覺得總是少了一點東西,少了什麼了,失去了Android中使用XML定義元件的便攜性,這種感覺讓人很不爽,呵呵,在這節裡我們會看到一個自定義View報錯的解決方法,讓我們來看看在XML中定義Vi

android定義view新增XML屬性

1.在value下新建檔案(檔名隨便),把需要的名稱和型別放進去 <?xml version="1.0" encoding="utf-8"?> <resources>

Android定義樣式與View的建構函式的第三個引數defStyle的意義

零、序 零、序   系統自帶的View可以在xml中配置屬性,對於寫的好的Custom View同樣可以在xml中配置屬性,為了使自定義的View的屬性可以在xml中配置,需要以下4個步驟: 通過<declare-styleable>為自定

Android定義檢視View

標籤: 前言 好長時間沒寫blog了,心裡感覺有點空蕩蕩的,今天有時間就來寫一個關於自定義檢視的的blog吧。關於這篇blog,網上已經有很多案例了,其實沒什麼難度的。但是我們在開發的過程中有時候會用到一些自定義的View以達到我們所需要的效果。其實網上的很多

Android定義View屬性,attr format取值型別

原文連結 1. reference:參考某一資源ID。 (1)屬性定義: <declare-styleable name = "名稱"> <attr name = "background" format = "reference" />

Android定義view屬性動畫一

Android 裡動畫是有一些分類的:動畫可以分為兩類:Animation 和 Transition;其中 Animation 又可以再分為 View Animation 和 Property Animation 兩類: View Animation 是純粹基於 framew

android定義view、新增定義屬性

-----自定義View的步驟---:1.寫一個類繼承View;在類中實現各種方法2.在xml佈局中使用自定義的控制元件,必須要寫全路徑,並且使用屬性時需要申明名稱空間;3.在res/values下建立atts.xml--宣告給那個view新增自定義屬性,4.實現這個構造方法

Android定義檢視View之---進階篇(Canvas的使用)

更多技術內容請移步:我的個人部落格一、前言今天是週日,昨天剛剛寫完了一篇關於如何搭建LNMP環境,讓自己可以DIY有個性的個人主頁:那麼今天,我們繼續來看一篇關於Android中的UI篇,如何自定義檢視View的進階篇,關於前奏篇之前已經寫過了,還沒有了解的同學可以去看看:h

iOS實現定義View屬性在interface builder裡面設定

自定義View的屬性要實現像系統的view那樣在Attribute Inspector設定然後在interface builder檢視效果,需要用到兩個關鍵字IBInspectable和IB_DESIGNABLE。 0> 通過User Defined Rumtime

android定義的dialog的EditText無法彈出輸入法解決方案

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);//彈出輸入法,並且寫在show()方法之後。 解決Dialog 消失,輸入法不消失的問題: 參考:https://blog.csd

Android定義DatePicker

先看一下效果 看這個圖很顯然就是一個DatePicker和一個TimePicker組合來實現的 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.a

定義View---設定 item

效果圖: 功能: 支援item寬高定製 支援字型大小、顏色、左邊距定製 支援小紅點是否顯示 支援下一頁跳轉圖示背景、寬高、左右邊距定製 使用 <com.astraea.india.ui.usercenter.views.Comm

Android定義View

在Android開發中,系統本身為我們提供了許多可供選擇的UI控制元件,但是在有些情況下也是需要自定義一些控制元件的。比如UI中的柱狀圖、餅圖等。而自定義View就需要明白它的原理了。 大體上分為三步onMeasure、onLayout、onDraw。大部分情況下我們只需要重寫兩個函式:onMe

Android定義Dialog樣式

public class MyMiddleDialog extends Dialog { private Context context; public MyMiddleDialog(Context context) { sup

android基礎--定義View

 1.自定義view繪製字串 import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graph

Android定義TabLayout指示器長度

效果圖: MainActivity.java檔案 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(@Nullabl

Android應用定義View繪製方法手冊

背景 這篇遲遲難產的文章算是對2015前半年的一個交代吧,那時候有一哥們要求來一發Android Canvas相關總結,這哥們還打賞了,實在不好意思,可是這事一放就給放忘了,最近群裡小夥伴催著說沒更新部落格,坐等更新啥的,隨先有這麼一篇Android應用開發超

定義View-設定padding沒有作用的原因及解決

@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int paddingLeft = getPaddingLeft(); int paddingRight = getPaddingRight()

Android定義滑動選中控制元件WheelView

WheelView a great functional custom WheelView with demo in dialog and bottomDialog,android 滾動選擇控制元件,滾動選擇器 ========= How to