1. 程式人生 > >android開發之自定義屬性、View和使用

android開發之自定義屬性、View和使用

“自定義”這三字聽起來就像是一個高階程式設計師所擁有的一樣!太不接地氣了!come on,baby,讓我們成為高階程式設計師吧!哈哈!

第一步:首先建立一個工程專案,在專案中的res/values/下建立atts.xml檔案,在該檔案中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="名稱">
        <attr name="title" format="string"/>
        <attr name="titleTextSize" format="dimension"/>
        <attr name="titleTextColor" format="color"/>
        <attr name="leftTextColor" format="color"/>
        <attr name="leftBackground" format="reference|color"/>
        <attr name="leftText" format="string"/>
        <attr name="rightTextColor" format="color"/>
        <attr name="rightBackground" format="reference|color"/>
        <attr name="rightText" format="string"/>
    </declare-styleable>
</resources>

值得注意的是,要記得寫<declare-styleable name="xxx">和<attr name="xxx" format="xxx">.

那麼,再此我還得說說:format它所擁有哪些型別以及如何使用它:

1. reference:參考某一資源ID。
    (1)屬性定義:
            <declare-styleable name = "名稱">
                   <attr name = "background" format = "reference" />
            </declare-styleable>
    (2)屬性使用:
             <ImageView
                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/圖片ID"
                     />
2. color:顏色值。
    (1)屬性定義:
            <declare-styleable name = "名稱">
                   <attr name = "textColor" format = "color" />
            </declare-styleable>
    (2)屬性使用:
            <TextView
                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:textColor = "#00FF00"
                     />
3. boolean:布林值。
    (1)屬性定義:
            <declare-styleable name = "名稱">
                   <attr name = "focusable" format = "boolean" />
            </declare-styleable>
    (2)屬性使用:
            <Button
                    android:layout_width = "42dip"
                    android:layout_height = "42dip"
                    android:focusable = "true"
                    />
4. dimension:尺寸值。
    (1)屬性定義:
            <declare-styleable name = "名稱">
                   <attr name = "layout_width" format = "dimension" />
            </declare-styleable>
    (2)屬性使用:
            <Button
                    android:layout_width = "42dip"
                    android:layout_height = "42dip"
                    />
5. float:浮點值。
    (1)屬性定義:
            <declare-styleable name = "AlphaAnimation">
                   <attr name = "fromAlpha" format = "float" />
                   <attr name = "toAlpha" format = "float" />
            </declare-styleable>
    (2)屬性使用:
            <alpha
                   android:fromAlpha = "1.0"
                   android:toAlpha = "0.7"
                   />
6. integer:整型值。
    (1)屬性定義:
            <declare-styleable name = "AnimatedRotateDrawable">
                   <attr name = "visible" />
                   <attr name = "frameDuration" format="integer" />
                   <attr name = "framesCount" format="integer" />
                   <attr name = "pivotX" />
                   <attr name = "pivotY" />
                   <attr name = "drawable" />
            </declare-styleable>
    (2)屬性使用:
            <animated-rotate
                   xmlns:android = "http://schemas.android.com/apk/res/android"  
                   android:drawable = "@drawable/圖片ID"  
                   android:pivotX = "50%"  
                   android:pivotY = "50%"  
                   android:framesCount = "12"  
                   android:frameDuration = "100"
                   />
7. string:字串。
    (1)屬性定義:
            <declare-styleable name = "MapView">
                   <attr name = "apiKey" format = "string" />
            </declare-styleable>
    (2)屬性使用:
            <com.google.android.maps.MapView
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
                    />
8. fraction:百分數。
    (1)屬性定義:
            <declare-styleable name="RotateDrawable">
                   <attr name = "visible" />
                   <attr name = "fromDegrees" format = "float" />
                   <attr name = "toDegrees" format = "float" />
                   <attr name = "pivotX" format = "fraction" />
                   <attr name = "pivotY" format = "fraction" />
                   <attr name = "drawable" />
            </declare-styleable>
    (2)屬性使用:
            <rotate
                   xmlns:android = "http://schemas.android.com/apk/res/android" 
               android:interpolator = "@anim/動畫ID"
                   android:fromDegrees = "0" 
               android:toDegrees = "360"
                   android:pivotX = "200%"
                   android:pivotY = "300%" 
               android:duration = "5000"
                   android:repeatMode = "restart"
                   android:repeatCount = "infinite"
                   />
9. enum:列舉值。
    (1)屬性定義:
            <declare-styleable name="名稱">
                   <attr name="orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr>            
            </declare-styleable>
    (2)屬性使用:
            <LinearLayout
                    xmlns:android = "http://schemas.android.com/apk/res/android"
                    android:orientation = "vertical"
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    >
            </LinearLayout>
10. flag:位或運算。
     (1)屬性定義:
             <declare-styleable name="名稱">
                    <attr name="windowSoftInputMode">
                            <flag name = "stateUnspecified" value = "0" />
                            <flag name = "stateUnchanged" value = "1" />
                            <flag name = "stateHidden" value = "2" />
                            <flag name = "stateAlwaysHidden" value = "3" />
                            <flag name = "stateVisible" value = "4" />
                            <flag name = "stateAlwaysVisible" value = "5" />
                            <flag name = "adjustUnspecified" value = "0x00" />
                            <flag name = "adjustResize" value = "0x10" />
                            <flag name = "adjustPan" value = "0x20" />
                            <flag name = "adjustNothing" value = "0x30" />
                     </attr>         
             </declare-styleable>
     (2)屬性使用:
            <activity
                   android:name = ".StyleAndThemeActivity"
                   android:label = "@string/app_name"
                   android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
                   <intent-filter>
                          <action android:name = "android.intent.action.MAIN" />
                          <category android:name = "android.intent.category.LAUNCHER" />
                   </intent-filter>
             </activity>
     注意:
     屬性定義時可以指定多種型別值。
    (1)屬性定義:
            <declare-styleable name = "名稱">
                   <attr name = "background" format = "reference|color" />
            </declare-styleable>
    (2)屬性使用:
             <ImageView
                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/圖片ID|#00FF00"
                     />
免責宣告:此處轉載http://www.cnblogs.com/tiantianbyconan/archive/2012/06/06/2538528.html

--------------------------------------自定義屬性先講到這裡,當然後面還會講,接下來將給大家介紹自定義View-----------------------------------

第一步:建立以類繼承View(及其它的子View),在這裡將繼承RelativeLayout,同時在類中宣告我們的自定義屬性:

public class TopBar extends RelativeLayout {

    private Button leftButton,rightButton;
    private TextView tv_title;
    
    private String title;
    private float titleTextSize;
    private int titleTextColor;
    
    private int leftTextColor;
    private Drawable leftBackground;
    private String leftText;
    
    private int rightTextColor;
    private Drawable rightBackground;
    private String rightText;
    
    public TopBar(Context context) {
        super(context);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

在此,我將和大家聊聊這三個構造方法的區別:
public void CustomView(Context context) {} //代號C1
public void CustomView(Context context, AttributeSet attrs) {} //代號C2
public void CustomView(Context context, AttributeSet attrs, int defStyle) {} //代號C3
C1:用code動態建立一個View而不使用佈局檔案xml inflate,那麼實現C1就可以了!

C2:多了一個AttributeSet型別的引數,在通過佈局檔案xml引用自定義VIew時,這個引數會將xml裡引用的自定義View所設定的屬性傳遞給建構函式;如果採用xml inflate的方法卻沒有在code裡實現C2,那麼執行時就會報錯!

C3:多了一個defStyle的int引數,關於這個引數doc裡是這樣描述的:

The default style to apply to this view. If 0, no style will be applied (beyond what is included in the theme). This may either be an attribute resource, whose value will be retrieved from the current theme, or an explicit style resource.

從字面上翻譯:這個引數似乎是用來指定View的預設的style,如果是0,那麼就不會應用任何預設(或者叫預設)的style。另外這個引數可以是一個屬性指定的style引用的,也可以直接是一個顯示 的style資源。

1、C3什麼時候被呼叫?

C1是程式碼建立View時,C2是xml建立View時,那麼C3呢?既然defStyle是一個與指定style有關的引數,那麼一個比較自然的猜想是,當在xml裡通過某種方式指定了View的Style時,C3在該View被inflate時呼叫,並將傳入給defStyle;

那麼,在xml裡面指定style有幾種方式呢?

第一種:直接在佈局檔案中該自定義View標籤裡使用:

style="@style/customstyle"

第二種:採用指定theme的方式,在AndroidManifest.xml的application標籤裡使用:
android:theme="@style/customstyle<span style="font-family: Arial, Helvetica, sans-serif;">"</span>
這兩種方式都需要在res/values./styles.xml裡定義customstyle:
<?xml version="1.0" encoding="utf-8"?>
<resources>
     <style name="customstyle">
         <item name="android:background">@drawable/bg</item>
         [... or other style code...]
     </style>
</resources>
注:使用theme時,標準的theme定義方式是把style放在themes.xml,但實際上R.java在生成時無論是themes.xml和style.xml裡的style都是同質的,都存在R.style下。

回到C3的問題上來,那麼這兩種指定style的方式會不會觸發C3呢?很遺憾,不會,那麼C3到底什麼時候呼叫呢?答案是當你顯示呼叫它的時候也就是通常在C1和C2裡呼叫:

public void CustomView(Context context, AttributeSet attrs) {
     this(context, attrs, resid);
}
通過這樣的方式將真正建構函式的實現轉移到C3裡,並由resid指定defStyle,作為預設style。

2、defStyle接受什麼樣的值?

在doc上說明:這個引數可以是一個屬性指定的style引用,也可以直接是一個顯示的style資源,那我們就試驗一下看看:

首先在res/values/styles.xml裡定義一個style:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="purple">
        <item name="android:background">#FFFF00FF</item>
    </style>
</resources>
然後自定義一個View(或者SurfaceView也是可以的),CustomView.java:
public class CustomView extends View {

    //C1
    public CustomView(Context context) {
        super(context);
    }

    //C2
    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    //C3
    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
}
接著在建立一份佈局檔案資源:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              <strong><span style="color:#ff0000;">xmlns:myxmlns="http://schemas.android.com/apk/res/com.zanelove.test"//引用自定義屬性請在此處宣告</span></strong>
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" >
     <com.zanelove.test.CustomView android:layout_width="100px"
                                   android:layout_height="100px" />
</LinearLayout>
最後在MainActivity中:
public class Test extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

接下來引用我們定義的style:
<com.zanelove.test.CustomView android:layout_width="100px"
                          android:layout_height="100px"
                          <strong><span style="color:#ff0000;">style="@style/purple"</span></strong>
/>

這樣的話View的背景變成了紫色,但如果你log一下就會發現,呼叫的還是C2。

下面我們就來研究defStyle到底接受什麼樣的引數:

首先把style和theme的引用都去掉,還原到黑色背景的View,這樣在程式裡R.style.purple就是這個style的顯式引用,那麼,理論上我們把R.style.purple當作defStyle傳給C3,是不是就能做到設定View預設背景為紫色呢?

public CustomView(Context context, AttributeSet attrs) {
    this(context, attrs, R.style.purple);
}
如果你log一下,就會發現,C2確實執行了,甚至R.style,purple也成功傳給C3裡的defStyle了,但是,View的背景還是黑色。

我們試試另外一種方式,傳入一個引用style資源的屬性(類似R.attr.buttonStyle)。這先要建立一個res/values/attrs.xml的檔案,這個檔案用來定義某個View裡可以出現的屬性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">
        <attr name="ourstyle" format="reference" />
        <attr name="atext" format="string" />
    </declare-styleable>
</resources>
現在我們為CustomView增加了兩個可以出現的自定義屬性,ourstyle和atext,前者就是我們打算用來引用一個style資源的屬性,後者是一個沒什麼用的字串屬性,放在這裡只是為了後面做測試。
現在我們就可以在程式裡引用這個屬性並把這個引數傳給defStyle。

當然,在這之前我們先要把purple這個style賦值給ourstyle。給一個view的屬性賦值,就和給android:layout_width賦值一樣,除了名稱空間不同(layout/main.xml的LinearLayout標籤裡有名稱空間的宣告):
<com.zanelove.test.CustomView android:layout_width="100px"
                          android:layout_height="100px"
                          myxmlns:ourstyle="@style/purple"
                          myxmlns:atext="test string"
/>
也可以用指定theme的方法,在theme裡給所有的CustomView都賦予一個相同的預設的ourstyle的值,然後應用這個theme:

子styles.xml裡另外定義一個style作為theme:

<style name="purpletheme">
    <item name="ourstyle">@style/purple</item>
</style>

在AndroidManifest.xml的application標籤中應用theme:
android:theme="style/purpletheme"
這兩種指定屬性的方法不同,在程式裡引用這個屬性的方法也不同。theme指定的屬性,可以直接用R.attr.ourstyle來引用,也可以用R.styleable.CustomView[R.styleable.CustomView_ourstyle]來引用,於是:
public CustomView(Context context, AttributeSet attrs) {
    this(context, attrs, R.attr.ourstyle );
}
這樣就成功地讓defStyle生效了。

那麼直接在標籤裡賦值的屬性怎麼引用呢?
直接在標籤裡賦值的屬性,都會在xml inflate時通過AttributeSet這個引數傳給C2,所以我們可以通過AttributeSet類提供的getAttributeResourceValue方法來獲取屬性的值。但是很可惜的是,我們只能獲取到屬性的值,而無法獲取包含這個值的屬性的引用(getAttributeNameResource方法返回的是和R.attr.ourstyle一樣的值,但這時R.attr.ourstyle並未指向@style/purple),這些亂七八糟的方法的各種值之間具體差別可以參考以下程式碼的log結果,相信仔細揣摩不難明白其中奧妙:

public CustomView(Context context, AttributeSet attrs) {
    this(context, attrs, attrs.getAttributeNameResource(2));
    String a1 = ((Integer)R.attr.ourstyle).toString();
    String a2 = ((Integer)R.styleable.CustomView_ourstyle).toString();
    String a3 = ((Integer)R.styleable.CustomView[R.styleable.CustomView_ourstyle]).toString();
    String a4 = ((Integer)R.style.purple).toString();
    String a5 = ((Integer)attrs.getAttributeNameResource(2)).toString();
    String a6 = ((Integer)attrs.getAttributeResourceValue(2,0)).toString();
    String a7 = ((Integer)R.attr.atext).toString();
    String a8 = ((Integer)R.styleable.CustomView[R.styleable.CustomView_atext]).toString();
    String a9 = attrs.getAttributeValue(2);
    String a10 = attrs.getAttributeValue(3);
}
log一下a1-10,示例結果如下:
a1 = 2130771968
a2 = 0
a3 = 2130771968
a4 = 2131034112
a5 = 2130771968
a6 = 2131034112
a7 = 2130771969
a8 = 2130771969
a9 = @2131034112
a10 = test string
凡是值相同的其實是一種意思,a1, a3, a5都指的是attrs.xml裡屬性的引用,這個引用id只有在theme裡賦值才有效,直接在標籤裡賦值是無效的。而傳這個id給defStyle就正符合doc裡寫的第一種情況。而a4, a6則直接代表了@style/purple的id,即doc裡寫的第二種情況,但是,回到我們最初的問題,傳R.style.purple,也就是2131034112這個id給defStyle是沒有效果的,為什麼呢?這個原因只能到android原始碼的view.java裡去看個究竟了:
public View(Context context, AttributeSet attrs, int defStyle) {
    this(context);
    TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,defStyle, 0);
    [...other code...]

這就是view基類的建構函式C3,它在接受defStyle引數後利用context.obtainStyledAttributes這個方法來構造一個完整的屬性陣列,幾個引數綜合起來提供了從主題、樣式裡繼承來的和直接在標籤裡定義的所有屬性,具體可以看這個方法的doc:


於是我就納悶了,顯式的資源呼叫難道不是應該通過defStyleRes這個引數麼?為什麼這裡直接就寫成0了呢?這裡寫成0,那當然defStyle只能採取defStryleAttr的方式了。google了一下,還真在android的google code project裡發現了一個developer提交的Issue 12683提到了這種情況,不過也沒人comment,不知道究竟是不是這樣……

免責宣告:此處轉載http://blog.csdn.net/z103594643/article/details/6755017#comments


----------------------------------------------------------好了,說了大半天終於把自定義View的三個構造方法說清楚了------------------------------------------

那麼,近下來就給大家繼續說說,如何將自定義View中的控制元件和自定義屬性聯絡在一起呢?之前我們有將屬性寫在atts.xml檔案當中,有自定義的View;那麼,如何將它們緊密聯絡在一起呢?

基本的思路就是:將在第二個引數的建構函式中,進行操作,接下來我們來看程式碼:

public class TopBar extends RelativeLayout {

    private Button leftButton,rightButton;
    private TextView tv_title;

    private String title;
    private float titleTextSize;
    private int titleTextColor;

    private int leftTextColor;
    private Drawable leftBackground;
    private String leftText;

    private int rightTextColor;
    private Drawable rightBackground;
    private String rightText;

    private LayoutParams leftParams,rightParams,titleParams;

    public TopBar(Context context) {
        super(context);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TopBar);
        leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor,0);
        leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
        leftText = ta.getString(R.styleable.TopBar_leftText);

        title = ta.getString(R.styleable.TopBar_title);
        titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor,0);
        titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize,0);

        rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
        rightTextColor = ta.getColor(R.styleable.TopBar_leftTextColor,0);
        rightText = ta.getString(R.styleable.TopBar_rightText);

        ta.recycle();//回收資源,避免快取資料錯誤

        leftButton = new Button(context);
        rightButton = new Button(context);
        tv_title = new TextView(context);
        
        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftBackground);
        leftButton.setText(leftText);

        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightBackground);
        rightButton.setText(rightText);

        tv_title.setText(title);
        tv_title.setTextColor(titleTextColor);
        tv_title.setTextSize(titleTextSize);
        tv_title.setGravity(Gravity.CENTER);

        setBackgroundColor(0xFFF59563);//設定背景顏色

        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        addView(leftButton,leftParams);

        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(rightButton,rightParams);

        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(tv_title,titleParams);
    }

    public TopBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
}
那麼,現在我們回憶一下,我們都做了什麼?

第一步:自定義屬性,在專案中的res/values/下建立atts.xml檔案,同時弄明白了format格式型別。

第二步:自定義View,並且在第二個引數的建構函式中將自定義屬性和控制元件緊密聯絡在了一起,同時詳細的講解了三個建構函式的區別和使用範圍。
          1、自定義View:首先繼承View或者子View。

          2、覆寫其構造方法,在其構造方法中將自定義屬性和值賦給自定義View中的控制元件。

          3、使用LayoutParams的方式把一個控制元件新增到ViewGroup中,而所有的佈局屬性都是在LayoutParams當中設定的。

第三步:引用自定義View和自定義屬性:

          1、引用自定義View:<com.zanelove.topbardemo.TopBar/>

          2、引用自定義屬性:

                 ①在Eclipse中:需要跟上完整的包名

                      xmlns:custom="http://schema.android.com/apk/res/com.zanelove.topbardemo //完整的包名

                 ②在Studio中:xmlns:custom="http://schema.android.com/apk/res-auto"//只需這樣寫即可

<?xml version="1.0" encoding="utf-8"?>
<!--引用自定義Viewxmlns:custom="http://schemas.android.com/apk/res-auto"-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:custom="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <!--引用自定義View:com.zanelove.topbardemo.TopBar-->
    <com.zanelove.topbardemo.TopBar
            android:id="@+id/topbar"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            custom:leftBackground="@drawable/ic_launcher"
            custom:leftText="Back"
            custom:leftTextColor="#ffffff"
            custom:rightBackground="@drawable/ic_launcher"
            custom:rightText="More"
            custom:rightTextColor="#ffffff"
            custom:title="自定義標題"
            custom:titleTextColor="#123456"
            custom:titleTextSize="10sp"
            />

</LinearLayout>

人總是那麼不知足,就想著:如何給leftButton和rightButton動態增加點選事件呢?
leftButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View view) {

   }
});

rightButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
                
    }
});
但是,這樣寫的話,就失去了作為模板的意義了!那如何讓子類動態的設定自個的點選事件呢?--答案很簡單,介面回撥機制!
public OnTopBarClickListener listener;
public interface OnTopBarClickListener{
    public void leftClick();
    public void rightClick();
}
public void setOnTopBarClickListener(OnTopBarClickListener listener){
    this.listener = listener;
}
leftButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        listener.leftClick();
    }
});

rightButton.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View view) {
         listener.rightClick();
     }
});

介面回撥機制實現了,那怎麼去使用呢?以上程式碼都是寫在自定義View類中,那麼,接下來的程式碼將寫在其他類中,進行對介面回撥的使用:

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //使用介面回撥
        TopBar topBar = (TopBar) this.findViewById(R.id.topbar);
        topBar.setOnTopBarClickListener(new TopBar.OnTopBarClickListener() {
            @Override
            public void leftClick() {
                //點選leftButton,處理事件
            }

            @Override
            public void rightClick() {
                //點選rightButton,處理事件
            }
        });

//        topBar.setLeftButtonIsVisible(true);
    }
}
在IT界中,懶惰,只為了更好的懶惰!

相關推薦

android開發定義屬性View使用

“自定義”這三字聽起來就像是一個高階程式設計師所擁有的一樣!太不接地氣了!come on,baby,讓我們成為高階程式設計師吧!哈哈! 第一步:首先建立一個工程專案,在專案中的res/values/下建立atts.xml檔案,在該檔案中: <?xml version

Android開發定義屬性的使用

自定義屬性一般會在我們自定義一個view的時候會用到,這個其實在系統應用中相當的常見,比如我目前維護的系統launcher應用,裡面就是相當多的自定義view會用到這個自定義屬性設定,那麼現將其總結總結。有些東西不去總結下來,時間久了真的會忘記。 步驟一:

android開發定義TextView設定字間距以及通過TextView控制元件屬性設定行間距

眾所周知,我們的TextView控制元件是沒有設定字間距的屬性滴!為了現實這一夢想,我玩起來了自定義TextView,從而來設定字間距: 自定義TextView設定字間距 第一步:建立自定義TextView: package com.zanel

Android 開發定義控制元件開發-01

最近一直在忙於公司的專案,因為要去現場測試正式使用,專案不大但是經手了三個人,到我這裡只能去填坑了,不說這個了,說一下今天得主題,自定義控制元件之基本圖形繪製。 我們平時畫圖需要兩種工具:紙和筆。在Android中 Paint 就是畫筆,而Canvas類就是紙,在這裡叫做畫布。 所以

Android 開發定義控制元件開發-02

1.畫筆的基本設定 : 1.setColor() 該函式的作用是設定畫筆顏色,完整的函式宣告如下: void setColor(int color) 我們知道,一種顏色是由紅、綠、藍三色合成出來的,所以引數 color 只能取8位的0xAARRGGBB樣式顏色值。 其中:

Android開發定義可清空內容的EditText

在開發過程中不可避免的總會遇到比如登入註冊、使用者資訊修改等,這時候又是不可避免的會用到EditText控制元件。這個控制元件的使用頻率雖然幾乎類似我們吃飯用“筷子”的頻率,but能不能用出花樣

Android開發定義控制元件--ViewPager

package com.itheima18.viewpager; import java.util.ArrayList; import java.util.Timer; import java.util.concurrent.Executors; import java.util.concurrent.Sc

Android開發定義圓角矩形圖片ImageView

android中的ImageView只能顯示矩形的圖片,這樣一來不能滿足我們其他的需求,比如要顯示圓角矩形的圖片,這個時候,我們就需要自定義ImageView了,其原理就是首先獲取到圖片的Bitmap,然後進行裁剪對應的圓角矩形的bitmap,然後在onDraw()進行繪製

Android開發定義控制元件(一)---onMeasure詳解

         話說一個有十年的程式設計經驗的老漢,決定改行書法,在一個熱火炎炎的中午,老漢拿著毛筆,在一張白紙上寫了個“Hello World!”,從此開啟了他的書法旅程。那麼問題來了請問自定義一個控制元件需要怎樣的流程?我們經常說自定義控制元件,那麼究竟怎樣去自定義一

Android學習 定義屬性及TypedArray的用法

  一、 背景說明:           在xml檔案裡定義控制元件的屬性,我們用的最多的是 Android系統自帶的屬性如: <ImageView android:id="@+id/iv_icon" android

Android定義View定義屬性

在Android開發中經常會用到自定義View的情況,而在自定義View時,自定義屬性是必須用到的。 1、新建一個自定義View如CustomView 它的自定義屬性主要是通過declare-styleable標籤為其配置自定義屬性。具體做法是:在res/values/目錄下增加一個reso

Android 基礎定義View使用技巧難點總結

1、簡介 該文章記錄的是本人在日常自定義View的一些常見技巧與問題的解決方案。 2、具體案例 2.1 探究畫筆的具體位置 情況1: // 繪製一個 描邊為10f的的一個矩形 @Override protected void onDraw(Canva

Android開發定義表情併發送出去經典的傳送表情

本文例項講述了Android程式設計開發實現輸入(自定義表情包)QQ表情影象併發送出去別人收到並解析出來的方法。分享給大家供大家參考,原來QQ微信等傳送表情其實發送的都不是表情,而是一個富文字,收到訊息後再解析得來的,具體效果如下 : 表情傳送出去是這樣:   最近在

Android——EditText定義邊框圓角其常用屬性總結

看下效果圖: 執行步驟: 首先在/res/layout資料夾下建立custom_et_layout.xml佈局檔案,原始碼如下: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:

Android軟體開發 定義控制元件

Android軟體開發之 自定義控制元件 雖然Android系統提供了各種各樣的控制元件供我們開發使用,但在實際的開發中,系統提供的控制元件有時候不能滿足我們的需求,這時我們就需要自定義一個控制元件。 下面的例子就來自定義一個簡單的Button: 首先是佈局,image_btn.xml: <?xml

Android開發:建立定義檢視–建立一個View

翻自:http://developer.android.com/training/custom-views/create-view.html 設計良好的定製檢視與其他精心設計的類相似。它用一種易於使用的介面封裝了一些特殊的方法集合,它高效使用記憶體和CPU等等。除了良好的設計以外,一個定製檢視應該: 符合

安卓開發定義View通過Drawable繪製圖標

//主要通過Drawable物件來繪製圖標 public class MyView extends View{ private float mCircleRadius; pri

Android定義控制元件封裝定義屬性的實現

在開發中有時候我們需要去自定義一些組合控制元件,而在使用過程中,又想要自己的組合控制元件能有原生控制元件那樣可以在xml中使用屬性控制,那麼我們就需要去自定義一些屬性了 1:首先在values/attrs.xml中進行屬性的定義 <?xml version="1.

Android基礎定義Application

ase man googl extends == attach 做到 不知道 ces Application Android提供了一個Application類,每當應用程序啟動時,系統會自動將這個類進行初始化。在項目中,我們在一些工具類采用了單例模式,其生命周期和整個應用程

springboot定義屬性以及亂碼三

自定義屬性的使用(讀取配置檔案,在專案啟動的時候根據@Value去配置檔案中獲取屬性) 在建好的springboot專案properties屬性中自定義屬性,如下: 通過@Value獲取自定義屬性 @Value("${name}") 啟動專案: