android 實現自訂群組件

來源:互聯網
上載者:User

android開發中,對於複用率較高的多個控制項,採用組件的方式可能更加方便,首先定義一個xml檔案:

檔案名稱:lyt_customer_service_phone.xml

 


然後在java代碼中建立一個自訂視圖,並且引用它:

 

檔案名稱:CustomerServicePhone.java

 

package cn.czl.view;import cn.czl.R;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.LinearLayout;public class CustomerServicePhone extends LinearLayout {    public CustomerServicePhone(final Context context, AttributeSet attrs) {        super(context, attrs);        ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(                R.layout.lyt_customer_service_phone, this);        setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                context.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(tel:4006601360)));            }        });    }    public CustomerServicePhone(Context context) {        super(context);    }}

這樣我們就建立成功了,實現了一個包含圖片的textview,點擊即可撥打到電話的組件,下面我們在xml中引用它:

 

 

                                                            

這樣我們在java代碼中,就不需要每次去定義這個視圖的動作了,實現了一個地方定義,多個地方引用的目的。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.