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

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

1、工程目录结构

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("自定义组合控件");
*/
    }
}

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

然后大家懂的,我就开始到处找错误,找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!终于得到想要的结果了!


时间: 2024-08-10 00:05:14

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

Android开发学习笔记之Activity属性设置

麦子学院android开发老师说Activity是Android组件中最基本也是最为常见用的四大组件之一,在android开发中运用极为广泛,作为android开发初学者需要熟练掌握,麦子学院android开发老师分享了Activity属性的常用设置. android:allowTaskReparenting 是否允许activity更换从属的任务,比如从短信息任务 切换到浏览器任务. android:alwaysRetainTaskState 是否保留状态不变, 比如切换回home, 再从新打

Android开发技巧之使用weight属性实现控件的按比例分配空间

从今天开始,把看书时候的知识点整理成博客, 这个比较简单,估计有经验的都用过,weight属性 在做Android布局的时候,经常遇到需要几个控件按比例分配空间的情况 比如下图效果 在底部设置两个button,占据底部宽度一部分的同时,保持1:3的比例, 当然了,这么难看的布局用处不大,仅是用来说明weight的用法 布局代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" x

Android开发新手需知EditText属性解析

麦子学院android开发谭老师说:才开始学习android开发时,android开发的基本知识是必须了解.记住的.熟练的.在android开发中,EditText继承TextView,所以EditText具有TextView的属性特点,下面主要介绍一些EditText的特有的输入法的属性特点 android:layout_gravity="center_vertical":设置控件显示的位置:默认top,这里居中显示,还有bottom android:hin:Text为空时显示的文字

android中GridView关于间距的属性值介绍

android:columnWidth  设置列的宽度.关联的方法为:setColumnWidth(int)  stretchMode属性值的作用是设置GridView中的条目以什么缩放模式去填充空间.参数stretchMode 可选值:NO_STRETCH,STRETCH_SPACING,STRETCH_SPACING_UNIFORM,或STRETCH_COLUMN_WIDTH android:gravity  设置此组件中的内容在组件中的位置.可选的值有:top.bottom.left.ri

Android开发自定义控件实现一个饼状图

实现一个如图所示的控件,包括两部分,左边的饼状图和中间的两个小方块,及右边的两行文字 实现起来比较简单,只是一些绘图API的调用 核心代码在onDraw函数里边,,对静态控件进行绘制即可 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /*饼状图的x坐标*/ float centreX= getWidth()/5; /*饼状图的y坐标*/ float centreY= getHeight()/2; /*

50一个Android开发技巧(01 利用好layout_weight属性)

问题:如何将一个Button放置在布局的中间,并设置其宽度parent的50%? 分析:问题想要达到的效果应该是这样: (原文地址:http://blog.csdn.net/vector_yi/article/details/24397733) 这看起来不难,但非常多开发人员并不知道达到这样效果的最佳方法. 解决:在此我们将weightSum属性与layout_weight属性一起利用. <LinearLayout xmlns:android = "http://schemas.andro

Android开发之文件保存读取

Android中文件保存读取可选择在手机本身存储.外存储如SD卡中进行,本文将依次介绍. 1.在手机本身存储保存.读取文件. 布局文件如下:     效果图如下: 在文件名框中填写保存时的文件名,文件内容框填写保存内容,点击保存后即可实现保存至手机本身存储. .java代码如下: MainActivity.java 此处的getApplicationContex()方法(生命周期是整个应用,用MainActivity.this代替时表示获取的是该Activity的上下文,生命周期为该Activi

android开发步步为营之54:读取assets,raw文件夹下文件

一.读取assets文件下文件products.json public String readAssetFile(Context c, String file) { Elapsed profiler = new Elapsed(); BufferedReader bufReader = null; try { InputStreamReader inputReader = new InputStreamReader(c.getResources().getAssets().open(file))

推荐一个Android开发懒人库 -- ButterKnife

ButterKnife -- 项目地址:https://github.com/JakeWharton/butterknife 都说程序员都是比较懒的,什么事情都想着让程序自动化帮忙减轻工作量,这个开源库可以让我们从大量的findViewById()和setonclicktListener()解放出来. 解放控件对象实例化 也就是 findViewById(),一直以来的做法都是一个个定义,然后在 setContentView() 或 inflate() 之后一一来findViewById()进行