解决一个 Android开发自定义控件问题,无法读取属性值
今天玩了一下Android自定义控件,是一个TextView和ImageButton的组合控件,所有的都写好了,但是运行得不到想要的结果,找了大半天找不到错误,代码如下:
1、工程目录结构
2、p_w_picpathbtn_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/p_w_picpathBtnImageBtnWithText"
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
packagecom.example.administrator.myview;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.drawable.Drawable;importandroid.util.AttributeSet;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.widget.ImageButton;importandroid.widget.LinearLayout;importandroid.widget.TextView;/***Createdby猿团Hockingon2016/2/23.*/publicclassImageBtnWithTextextendsLinearLayout{privateImageButtonmBtn=null;privateTextViewmTv=null;publicImageBtnWithText(Contextcontext,AttributeSetattrs){super(context,attrs);Viewview=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);TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.ImageBtnWithText);CharSequencetext=a.getText(R.styleable.ImageBtnWithText_text);if(text!=null)mTv.setText(text);Drawabledrawable=a.getDrawable(R.styleable.ImageBtnWithText_src);if(drawable!=null)mBtn.setImageDrawable(drawable);a.recycle();}publicvoidsetImageResrouce(intresId){mBtn.setImageResource(resId);}publicvoidsetText(Stringtext){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/p_w_picpathBtnBtnWithText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义控件TextView和ImageButton组合"
android:src="@drawable/logo"
/>
</RelativeLayout>
6、MainActivity.java
packagecom.example.administrator.myview;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;publicclassMainActivityextendsAppCompatActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ImageBtnWithTextii=(ImageBtnWithText)findViewById(R.id.p_w_picpathBtnBtnWithText);/*ii.setImageResrouce(R.drawable.logo);ii.setText("自定义组合控件");*/}}
好了,到此为止全部程序贴完了,本以为能得到想要的结果,但是一运行竟然啥都没有····是个空白!
然后大家懂的,我就开始到处找错误,找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/p_w_picpathBtnBtnWithText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
myview:text="自定义控件TextView和ImageButton组合"
myview:src="@drawable/logo"
/>
</RelativeLayout>
然后再运行工程,MyGod!终于得到想要的结果了!
附:源码 MyView.zip http://down.51cto.com/data/2184366
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。