Android自訂群組合控制項(一)

來源:互聯網
上載者:User

Android自訂群組合控制項(一)

最近參加濟南一分享會,感受頗深,也很欣賞大神們的分享精神,好東西大家一起分享

不能做只會搬磚的碼農,要成為一個真正的程式員,當然我是媛,程式員分為三類,初級程式員,中級程式員,進階程式員,也不能總是做個小菜鳥,那也太沒有追求了,為了我的大神夢,開始學習自訂控制項

本部落格從最簡單的開始,先來介紹自訂群組合控制項,在我看來自訂空間中群組控制項是最簡單的,這裡拿最常見的圖片和文字組合來說明

先上介面:


這個介面還是很常見的,很多app首頁都是這麼介面,當然下面的圖片文字等是可以自己設定的

下面是自訂群組合控制項的主要代碼:

首先是繼承自LinearLayout自訂控制項

package com.sdufe.thea.guo.view;import android.content.Context;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import com.sdufe.thea.guo.R;public class CustomView extends LinearLayout {private ImageView mImageView;private TextView mTextView;Context context;public CustomView(Context context) {super(context,null);}public CustomView(Context context, AttributeSet attrs) {super(context, attrs);this.context=context;View view=LayoutInflater.from(context).inflate(R.layout.custom_view, this,true);mTextView=(TextView) view.findViewById(R.id.custom_textview);mImageView=(ImageView) view.findViewById(R.id.custom_imageview);}public void setText(String text){mTextView.setText(text);}public void setTextColor(int color){mTextView.setTextColor(color);}public void setTextSize(float size){mTextView.setTextSize(size);}public void setImagViewResouce(Drawable drawable){mImageView.setImageDrawable(drawable);}}

然後是自訂控制項的布局檔案

                

最後是對自訂控制項的使用

                                        

package com.sdufe.thea.guo;import com.sdufe.thea.guo.view.CustomView;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Toast;public class MainActivity extends Activity {private CustomView mCustomView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initView();}private void initView() {mCustomView=(CustomView) findViewById(R.id.custom);mCustomView.setText("我是大壞蛋");mCustomView.setTextColor(getResources().getColor(R.color.text_color));mCustomView.setImagViewResouce(getResources().getDrawable(R.drawable.phone));mCustomView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getApplicationContext(), "別點我", Toast.LENGTH_LONG).show();}});}}

以上就是全部代碼了,這裡講一下我遇到的問題,一般我在填充布局的時候都用LayoutInflater.from(context).inflate(resource, root),可是自訂時,它就是顯示不出來,本著程式員精神,就使勁去查,查了查源碼,換成LayoutInflater.from(context).inflate(R.layout.custom_view, this,true);就可以了,恩,成功顯示出來了,挺開心,順便看了看源碼

一般LayoutInflater.from(context).inflate(resource, root),第一個參數就是要載入的布局id,第二個參數是指給該布局的外部再嵌套一層父布局,如果不需要就直接傳null,但是這裡要用到他的屬性,明顯不滿足,那就只能用有三個參數的了,那就來說一說三個參數的LayoutInflater.from(context).inflate(R.layout.custom_view, this,true);第一個參數還是載入的布局id,第二個參數是指給該布局的外部再嵌套一層父布局,第三個官方說attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.中文意思就是attachToRoot無論充氣層次應附加到根參數?如果為假,根僅用於建立的LayoutParams正確的子類中的XML根視圖.

public View inflate(int resource, ViewGroup root, boolean attachToRoot) {        if (DEBUG) System.out.println("INFLATING from resource: " + resource);        XmlResourceParser parser = getContext().getResources().getLayout(resource);        try {            return inflate(parser, root, attachToRoot);        } finally {            parser.close();        }    }
當繼續追蹤第五行代碼,你就會知道其實他的實質是利用XML中的Pull解析,這裡先暫時放一放,只是說明一下這三個參數

1.當root為空白時,attactRoot無效

2.當root不為空白時,attachRoot為TRUE會載入布局檔案的在外層root

3.當root不為空白,attachroot為FALSE時,不會載入最外層的布局檔案

ok,就先介紹這些,本部落客要是從Java代碼中實現自訂,下篇打算從屬性上實現自訂群組合控制項


代碼:http://download.csdn.net/detail/elinavampire/8141963

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.