解決一個 Android開發自訂控制項問題,無法讀取屬性值

來源:互聯網
上載者:User

標籤:android   開發   自訂控制項   

今天玩了一下Android自訂控制項,是一個TextView和ImageButton的群組控制項,所有的都寫好了,但是運行得不到想要的結果,找了大半天找不到錯誤,代碼如下:


1、工程目錄結構

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/7B/9A/wKioL1bNtd6T3tINAACaQ6j3EXQ725.png" title="1.png" alt="wKioL1bNtd6T3tINAACaQ6j3EXQ725.png" />

2、imagebtn_with_text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent" android:layout_height="match_parent"
   
android:background="#f5f5f5"
   
android:gravity="center">
<TextView
   
android:id="@+id/tvImageBtnWithText"
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content" />
   <ImageButton
       
android:id="@+id/imageBtnImageBtnWithText"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content" />
</LinearLayout>

3、attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable
name="ImageBtnWithText">
       <attr
name="text" format="string"/>
       <attr
name="src" format="reference" />
   </declare-styleable>
</resources>

4、ImageBtnWithText.java

package com.example.administrator.myview;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageButton;import android.widget.LinearLayout;import android.widget.TextView;/** * Created by 猿團Hocking on 2016/2/23. */public class ImageBtnWithText  extends LinearLayout {    private ImageButton mBtn =null;    private TextView  mTv = null;    public ImageBtnWithText(Context context, AttributeSet attrs) {        super(context, attrs);        View view = LayoutInflater.from(context).inflate(R.layout.imagebtn_with_text,this,true);        mTv = (TextView)view.findViewById(R.id.tvImageBtnWithText);        mBtn = (ImageButton) view.findViewById(R.id.imageBtnImageBtnWithText);        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageBtnWithText);        CharSequence  text = a.getText(R.styleable.ImageBtnWithText_text);        if(text!= null)  mTv.setText(text);        Drawable  drawable = a.getDrawable(R.styleable.ImageBtnWithText_src);        if(drawable!=null) mBtn.setImageDrawable(drawable);        a.recycle();    }   public void  setImageResrouce(int resId)    {        mBtn.setImageResource(resId);    }    public  void setText(String  text)    {        mTv.setText(text);    }}

5、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<com.example.administrator.myview.ImageBtnWithText
   
android:id="@+id/imageBtnBtnWithText"
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:text="自訂控制項TextView和ImageButton組合"
   
android:src="@drawable/logo"
 
/>
</RelativeLayout>

6、MainActivity.java

package com.example.administrator.myview;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ImageBtnWithText ii = (ImageBtnWithText)findViewById(R.id.imageBtnBtnWithText);       /* ii.setImageResrouce(R.drawable.logo);        ii.setText("自訂群組合控制項");*/    }}



好了,到此為止全部程式貼完了,本以為能得到想要的結果,但是一運行竟然啥都沒有····是個空白!

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/7B/9D/wKiom1bNt9ayT4gSAAIoEZykXq4729.png" title="2.png" alt="wKiom1bNt9ayT4gSAAIoEZykXq4729.png" />


然後大家懂的,我就開始到處找錯誤,找bug,但是找了大半天都找不到錯誤所在,總是擷取不到兩個組件屬性所對應的值,也在網上查了好多資料,也看到好多類似的問題,但是都找不到答案,這下把我快弄瘋掉了!大家可知道哪裡錯了?



終於·····

查閱官網API,終於找到答案了!問題出現在activity_main.xml中,

在布局檔案中使用:在使用之前必須聲名命名空間,xmlns:example="http://schemas.android.com/apk/res/com.example.administrator.myview"
說明:xmlns      是XML name space 的縮寫; 
          example   可為任意寫符       
          http://schemas.android.com/apk/res/    此為android固定格式;                                           com.example.administrator.myview    此應用的包名,如manifest設定檔中一致。

於是將activity_main.xml 作了如下修改:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<com.example.administrator.myview.ImageBtnWithText
   xmlns:myview="http://schemas.android.com/apk/res/com.example.administrator.myview"
   android:id="@+id/imageBtnBtnWithText"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   myview:text="自訂控制項TextView和ImageButton組合"
   myview:src="@drawable/logo"
  />
</RelativeLayout>


然後再運行工程,MyGod!終於得到想要的結果了!


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/7B/9D/wKiom1bNumDQM7xxAAGOY18VBc4287.png" title="3.png" alt="wKiom1bNumDQM7xxAAGOY18VBc4287.png" />

本文出自 “Hocking大神” 部落格,請務必保留此出處http://hocking.blog.51cto.com/1100162/1744800

解決一個 Android開發自訂控制項問題,無法讀取屬性值

聯繫我們

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