RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
解决一个Android开发自定义控件问题,无法读取属性值

今天玩了一下Android自定义控件,是一个TextView和ImageButton的组合控件,所有的都写好了,但是运行得不到想要的结果,找了大半天找不到错误,代码如下:

创新互联是专业的黄山网站建设公司,黄山接单;提供成都网站建设、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行黄山网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

1、工程目录结构

解决一个 Android开发自定义控件问题,无法读取属性值

2、p_w_picpathbtn_with_text.xml


   android:layout_width="match_parent"android:layout_height="match_parent"
   android:background="#f5f5f5"
   android:gravity="center">
   android:id="@+id/tvImageBtnWithText"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>
          android:id="@+id/p_w_picpathBtnImageBtnWithText"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>

3、attrs.xml



   
       
       
   

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.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


   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">

   android:id="@+id/p_w_picpathBtnBtnWithText"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="自定义控件TextView和ImageButton组合"
   android:src="@drawable/logo"
 />

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.p_w_picpathBtnBtnWithText);
       /* ii.setImageResrouce(R.drawable.logo);
        ii.setText("自定义组合控件");
*/
    }
}

好了,到此为止全部程序贴完了,本以为能得到想要的结果,但是一运行竟然啥都没有····是个空白!

解决一个 Android开发自定义控件问题,无法读取属性值

然后大家懂的,我就开始到处找错误,找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 作了如下修改:


   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">

   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"
 />

然后再运行工程,MyGod!终于得到想要的结果了!


解决一个 Android开发自定义控件问题,无法读取属性值


附:源码  MyView.zip  http://down.51cto.com/data/2184366



本文标题:解决一个Android开发自定义控件问题,无法读取属性值
标题URL:http://sczitong.cn/article/gsdesi.html