http://blog.csdn.net/yanzi1225627/article/details/8633872

第二种方法也要新建一个图片+文字的xml布局文件,然后写一个类继承自LinearLayout。在主程序里实例化并设置相应参数。这种方式也是我最推荐的一种。

第一部分:myimgbtn_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:alpha="20"
  6. android:background="#87CE"
  7. android:orientation="vertical"
  8. >
  9. <ImageView
  10. android:id="@+id/img"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_gravity="center_horizontal"
  14. android:paddingBottom="5dip"
  15. android:paddingTop="5dip" />
  16. <TextView
  17. android:id="@+id/text"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:textColor="#FF6100"
  21. android:textSize="30dip"
  22. android:layout_gravity="center_vertical"/>
  23. </LinearLayout>

第二部分,与之布局相对应的MyImgBtn.java 文件:

  1. package yan.guoqi.testimgbtn;
  2. import android.content.Context;
  3. import android.graphics.Color;
  4. import android.util.AttributeSet;
  5. import android.view.LayoutInflater;
  6. import android.view.MotionEvent;
  7. import android.view.View;
  8. import android.view.View.OnTouchListener;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  11. import android.widget.TextView;
  12. public class MyImgBtn extends LinearLayout {
  13. private ImageView mImgView = null;
  14. private TextView mTextView = null;
  15. private Context mContext;
  16. public MyImgBtn(Context context, AttributeSet attrs) {
  17. super(context, attrs);
  18. // TODO Auto-generated constructor stub
  19. LayoutInflater.from(context).inflate(R.layout.myimgbtn_layout, this, true);
  20. mContext = context;
  21. mImgView = (ImageView)findViewById(R.id.img);
  22. mTextView = (TextView)findViewById(R.id.text);
  23. }
  24. /*设置图片接口*/
  25. public void setImageResource(int resId){
  26. mImgView.setImageResource(resId);
  27. }
  28. /*设置文字接口*/
  29. public void setText(String str){
  30. mTextView.setText(str);
  31. }
  32. /*设置文字大小*/
  33. public void setTextSize(float size){
  34. mTextView.setTextSize(size);
  35. }
  36. //     /*设置触摸接口*/
  37. //    public void setOnTouch(OnTouchListener listen){
  38. //        mImgView.setOnTouchListener(listen);
  39. //        //mTextView.setOnTouchListener(listen);
  40. //    }
  41. }

第三部分,主布局main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:background="@drawable/main_background2">
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello" />
  11. <yan.guoqi.testimgbtn.MyImgBtn
  12. android:id="@+id/MyIBtn_1"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_gravity="center_horizontal"
  16. android:clickable="true"
  17. android:focusable="true"
  18. />
  19. </LinearLayout>

第四部分,主程序:

  1. package yan.guoqi.testimgbtn;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Toast;
  7. public class TestImgBtnActivity extends Activity {
  8. private MyImgBtn MyIBtn1 = null;
  9. /** Called when the activity is first created. */
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. MyIBtn1 = (MyImgBtn)findViewById(R.id.MyIBtn_1);
  15. MyIBtn1.setImageResource(R.drawable.ic_launcher);
  16. MyIBtn1.setText("欢迎光临");
  17. MyIBtn1.setTextSize(24.0f);
  18. //MyIBtn1.setOnTouch(new MyOnTouchListener());
  19. MyIBtn1.setOnClickListener(new OnClickListener() {
  20. public void onClick(View arg0) {
  21. // TODO Auto-generated method stub
  22. Toast.makeText(TestImgBtnActivity.this,
  23. "您好",
  24. Toast.LENGTH_SHORT)
  25. .show();
  26. }
  27. });
  28. }
  29. }

这种方法很直观简单,与之第一种用Gallery方法而言更容易理解。就是自定义一个类,第一种方法虽然不用自定义类,但是Gallery相关的适配器配置和那个View相关的如果第一次会不大习惯。这种效果也不错,图就不贴了。尤其适合做那种背景是纯色,里面嵌套图片+文字。就是360手机安全卫士的主窗口,大家可以看下。应该就是为这种方式做的。美中不足的是,360手机安全卫士的主窗口里,你点击一下,背景会变。也就是说这还缺少个onTouchListener,稍后我补上。

Android自定义“图片+文字”控件四种实现方法之 二--------个人最推荐的一种的更多相关文章

  1. (转载)Android自定义标签列表控件LabelsView解析

    Android自定义标签列表控件LabelsView解析 作者 donkingliang 关注 2017.03.15 20:59* 字数 759 阅读 406评论 0喜欢 3 无论是在移动端的App, ...

  2. android 自定义空间 组合控件中 TextView 不支持drawableLeft属性

    android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...

  3. Android实现图片滚动控件,含页签功能,让你的应用像淘宝一样炫起来

    首先题外话,今天早上起床的时候,手滑一下把我的手机甩了出去,结果陪伴我两年半的摩托罗拉里程碑一代就这么安息了,于是我今天决定怒更一记,纪念我死去的爱机. 如果你是网购达人,你的手机上一定少不了淘宝客户 ...

  4. MFC入门(三)-- MFC图片/文字控件(循环显示文字和图片的小程序)

    惯例附上前几个博客的链接: MFC入门(一)简单配置:http://blog.csdn.net/zmdsjtu/article/details/52311107 MFC入门(二)读取输入字符:http ...

  5. Android中通过WebView控件实现与JavaScript方法相互调用的地图应用

    在Android中通过WebView控件,可以实现要加载的页面与Android方法相互调用,我们要实现WebView中的addJavascriptInterface方法,这样html才能调用andro ...

  6. UIButton图片文字控件位置自定义(图片居右文字居左、图片居中文字居中、图片居左文字消失等)

    在开发中经常会碰到需要对按钮中的图片文字位置做调整的需求.第一种方式是通过设置按钮中图片文字的偏移量.通过方法setTitleEdgeInsets和setImageEdgeInsets实现 代码如下: ...

  7. Android 自定义View修炼-如何打造Android自定义的下拉列表框控件

    一.概述 Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求, 比如有时候我们需要类似windows 或者web网页中常见的那种下拉列表控件,类似下图这样的 ...

  8. Android自定义View和控件之一-定制属于自己的UI

    照例,拿来主义.我的学习是基于下面的三篇blog.前两是基本的流程,第三篇里有比较细致的绘制相关的属性.第4篇介绍了如何减少布局层次来提高效率. 1. 教你搞定Android自定义View 2. 教你 ...

  9. Android自定义控件进阶-打造Android自定义的下拉列表框控件

    技术:Android+java   概述 Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求, 比如有时候我们需要类似windows 或者web网页中常见的 ...

随机推荐

  1. Central Europe Regional Contest 2012 Problem c: Chemist’s vows

    字符串处理的题目: 学习了一下string类的一些用法: 这个代码花的时间很长,其实可以更加优化: 代码: #include<iostream> #include<string> ...

  2. 【BZOJ 1597】 [Usaco2008 Mar]土地购买 (斜率优化)

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3601  Solved: 1322 Descrip ...

  3. java中的单例模式与doublecheck

    转自: http://devbean.blog.51cto.com/448512/203501 在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容易出现问题.下面就单 ...

  4. Yii处理流程

    Yii的应用程序处理流程 用户访问URL http://www.example.com/blog/index.php?r=site/contact 1.入口脚本被网站服务器执行以处理此请求. 2.一个 ...

  5. linux使用man命令后退出

    linux使用man命令后 使用q退出

  6. hadoop2.2编程:DFS API 操作

    1. Reading data from a hadoop URL 说明:想要让java从hadoop的dfs里读取数据,则java 必须能够识别hadoop hdfs URL schema, 因此我 ...

  7. POJ_3666_Making_the_Grade_(动态规划)

    描述 http://poj.org/problem?id=3666 给一串坡的高度,现在要调整某些点,使整个坡单调不降或单调不升.调整的花费为原高度与先高度的差的绝对值,问最小花费(可单增可单降). ...

  8. ☀【CSS3】icon

    Navicon Transformicons: Animated Navigation Icons with CSS Transformshttp://sarasoueidan.com/blog/na ...

  9. 【转】你应该知道的十个VirtualBox技巧与高级特性

    原文网址:http://www.searchvirtual.com.cn/showcontent_76463.htm VirtualBox集成的许多功能你可能从来没有使用过,即使你经常用它来运行虚拟机 ...

  10. Android-adb shell 读取手机系统文件

    1.首先保证手机是root 状态 2.运行 adb shell 页面以后 su root 3.ls 就会发现目录结构可以显示了