下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】解决一个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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年收银台项目创业投资方案
- 2024年防水涂料项目招商引资报告
- 2024年年中国食品饮料项目经营分析报告
- 2024个人装修泥工合同范本
- 2024年安全员考试题含答案(考试直接用)
- 2024年安全员考试题含答案(基础题)
- 2024 年全国保密知识竞赛抢答题库【历年真题】
- 2024安全员(初级)考试题模拟考试题库带答案(满分必刷)
- 2024年保密法知识竞赛试题附参考答案【研优卷】
- 2024年国家安全员资格考试重点题库带答案解析
- 2020部编版五年级语文下册第七单元知识梳理
- 福州长乐国际机场资产重组方案(讨论稿)
- 行星齿轮计算
- 医院缩短平均住院日的具体措施
- 空气洁净度等级标准及规范相关
- 环保管理制度
- 2018高考理综答题卡模板(最新整理)
- 公称压力(MPa)管道壁厚对照表
- 投标书范本商务标(word版)
- 研发部样品采购流程
- 110kV线路感应电势计算
评论
0/150
提交评论