【移動應用開發技術】解決一個 Android開發自定義控件問題無法讀取屬性值_第1頁
【移動應用開發技術】解決一個 Android開發自定義控件問題無法讀取屬性值_第2頁
【移動應用開發技術】解決一個 Android開發自定義控件問題無法讀取屬性值_第3頁
【移動應用開發技術】解決一個 Android開發自定義控件問題無法讀取屬性值_第4頁
【移動應用開發技術】解決一個 Android開發自定義控件問題無法讀取屬性值_第5頁
免費預覽已結束,剩余1頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

【移動應用開發技術】解決一個Android開發自定義控件問題,無法讀取屬性值

今天玩了一下Android自定義控件,是一個TextView和ImageButton的組合控件,所有的都寫好了,但是運行得不到想要的結果,找了大半天找不到錯誤,代碼如下:1、工程目錄結構2、p_w_picpathbtn_with_text.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/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/p_w_picpathBtnImageBtnWithText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/></LinearLayout>3、attrs.xml<?xmlversion="1.0"encoding="utf-8"?><resources>

<declare-styleablename="ImageBtnWithText">

<attrname="text"format="string"/>

<attrname="src"format="reference"/>

</declare-styleable></resources>4、ImageBtnWithText.javapackage

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.p_w_picpathbtn_with_text,this,true);

mTv

=

(TextView)view.findViewById(R.id.tvImageBtnWithText);

mBtn

=

(ImageButton)

view.findViewById(R.id.p_w_picpathBtnImageBtnWithText);

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<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="/apk/res/android"

xmlns:tools="/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/p_w_picpathBtnBtnWithText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="自定義控件TextView和ImageButton組合"

android:src="@drawable/logo"

/></RelativeLayout>6、MainActivity.javapackage

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.p_w_picpathBtnBtnWithText);

/*

ii.setImageResrouce(R.drawable.logo);

ii.setText("自定義組合控件");

*/

}

}好了,到此為止全部程序貼完了,本以為能得到想要的結果,但是一運行竟然啥都沒有····是個空白!然后大家懂的,我就開始到處找錯誤,找bug,但是找了大半天都找不到錯誤所在,總是獲取不到兩個組件屬性所對應的值,也在網上查了好多資料,也看到好多類似的問題,但是都找不到答案,這下把我快弄瘋掉了!大家可知道哪里錯了?終于·····查閱官網API,終于找到答案了!問題出現在activity_main.xml中,在布局文件中使用:在使用之前必須聲名命名空間,xmlns:example="/apk/res/com.example.administrator.myview"說明:xmlns

是XMLnamespace的縮寫;

example

可為任意寫符

/apk/res/

此為android固定格式;

com.example.administrator.myview

此應用的包名,如manifest配置文件中一致。于是將activity_main.xml作了如下修改:<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="/apk/res/android"

xmlns:tools="/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="/apk/res/com.example.administrator.myview"

android:id="@+id/p_w_picp

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論