Android自定义“图片+文字”控件四种实现方法之 二--------个人最推荐的一种
http://blog.csdn.net/yanzi1225627/article/details/8633872
第二种方法也要新建一个图片+文字的xml布局文件,然后写一个类继承自LinearLayout。在主程序里实例化并设置相应参数。这种方式也是我最推荐的一种。
第一部分:myimgbtn_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:alpha="20"
- android:background="#87CE"
- android:orientation="vertical"
- >
- <ImageView
- android:id="@+id/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:paddingBottom="5dip"
- android:paddingTop="5dip" />
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#FF6100"
- android:textSize="30dip"
- android:layout_gravity="center_vertical"/>
- </LinearLayout>
第二部分,与之布局相对应的MyImgBtn.java 文件:
- package yan.guoqi.testimgbtn;
- import android.content.Context;
- import android.graphics.Color;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class MyImgBtn extends LinearLayout {
- private ImageView mImgView = null;
- private TextView mTextView = null;
- private Context mContext;
- public MyImgBtn(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- LayoutInflater.from(context).inflate(R.layout.myimgbtn_layout, this, true);
- mContext = context;
- mImgView = (ImageView)findViewById(R.id.img);
- mTextView = (TextView)findViewById(R.id.text);
- }
- /*设置图片接口*/
- public void setImageResource(int resId){
- mImgView.setImageResource(resId);
- }
- /*设置文字接口*/
- public void setText(String str){
- mTextView.setText(str);
- }
- /*设置文字大小*/
- public void setTextSize(float size){
- mTextView.setTextSize(size);
- }
- // /*设置触摸接口*/
- // public void setOnTouch(OnTouchListener listen){
- // mImgView.setOnTouchListener(listen);
- // //mTextView.setOnTouchListener(listen);
- // }
- }
第三部分,主布局main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:background="@drawable/main_background2">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <yan.guoqi.testimgbtn.MyImgBtn
- android:id="@+id/MyIBtn_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:clickable="true"
- android:focusable="true"
- />
- </LinearLayout>
第四部分,主程序:
- package yan.guoqi.testimgbtn;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Toast;
- public class TestImgBtnActivity extends Activity {
- private MyImgBtn MyIBtn1 = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- MyIBtn1 = (MyImgBtn)findViewById(R.id.MyIBtn_1);
- MyIBtn1.setImageResource(R.drawable.ic_launcher);
- MyIBtn1.setText("欢迎光临");
- MyIBtn1.setTextSize(24.0f);
- //MyIBtn1.setOnTouch(new MyOnTouchListener());
- MyIBtn1.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- Toast.makeText(TestImgBtnActivity.this,
- "您好",
- Toast.LENGTH_SHORT)
- .show();
- }
- });
- }
- }
这种方法很直观简单,与之第一种用Gallery方法而言更容易理解。就是自定义一个类,第一种方法虽然不用自定义类,但是Gallery相关的适配器配置和那个View相关的如果第一次会不大习惯。这种效果也不错,图就不贴了。尤其适合做那种背景是纯色,里面嵌套图片+文字。就是360手机安全卫士的主窗口,大家可以看下。应该就是为这种方式做的。美中不足的是,360手机安全卫士的主窗口里,你点击一下,背景会变。也就是说这还缺少个onTouchListener,稍后我补上。
Android自定义“图片+文字”控件四种实现方法之 二--------个人最推荐的一种的更多相关文章
- (转载)Android自定义标签列表控件LabelsView解析
Android自定义标签列表控件LabelsView解析 作者 donkingliang 关注 2017.03.15 20:59* 字数 759 阅读 406评论 0喜欢 3 无论是在移动端的App, ...
- android 自定义空间 组合控件中 TextView 不支持drawableLeft属性
android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...
- Android实现图片滚动控件,含页签功能,让你的应用像淘宝一样炫起来
首先题外话,今天早上起床的时候,手滑一下把我的手机甩了出去,结果陪伴我两年半的摩托罗拉里程碑一代就这么安息了,于是我今天决定怒更一记,纪念我死去的爱机. 如果你是网购达人,你的手机上一定少不了淘宝客户 ...
- MFC入门(三)-- MFC图片/文字控件(循环显示文字和图片的小程序)
惯例附上前几个博客的链接: MFC入门(一)简单配置:http://blog.csdn.net/zmdsjtu/article/details/52311107 MFC入门(二)读取输入字符:http ...
- Android中通过WebView控件实现与JavaScript方法相互调用的地图应用
在Android中通过WebView控件,可以实现要加载的页面与Android方法相互调用,我们要实现WebView中的addJavascriptInterface方法,这样html才能调用andro ...
- UIButton图片文字控件位置自定义(图片居右文字居左、图片居中文字居中、图片居左文字消失等)
在开发中经常会碰到需要对按钮中的图片文字位置做调整的需求.第一种方式是通过设置按钮中图片文字的偏移量.通过方法setTitleEdgeInsets和setImageEdgeInsets实现 代码如下: ...
- Android 自定义View修炼-如何打造Android自定义的下拉列表框控件
一.概述 Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求, 比如有时候我们需要类似windows 或者web网页中常见的那种下拉列表控件,类似下图这样的 ...
- Android自定义View和控件之一-定制属于自己的UI
照例,拿来主义.我的学习是基于下面的三篇blog.前两是基本的流程,第三篇里有比较细致的绘制相关的属性.第4篇介绍了如何减少布局层次来提高效率. 1. 教你搞定Android自定义View 2. 教你 ...
- Android自定义控件进阶-打造Android自定义的下拉列表框控件
技术:Android+java 概述 Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求, 比如有时候我们需要类似windows 或者web网页中常见的 ...
随机推荐
- POJ 1905 Expanding Rods 二分答案几何
题目:http://poj.org/problem?id=1905 恶心死了,POJ的输出一会要lf,一会要f,而且精度1e-13才过,1e-12都不行,错了一万遍终于对了. #include < ...
- linux vi 使用
vi 有一般模式和编辑模式 如vi test.txt 是首先进入的一般模式,一般模式下只能进行复制.删除.粘贴文件数据, 在一般模式下按i .I.a.A.o.O 都能进入编辑模式,按下不同的键进入编辑 ...
- 2、.net NVelocity中原生javascript ajax封装使用
在页面上,我们经常会遇到局部刷新的例子,这个时候,就需要用到ajax, 因为很多代码都是公用的,所以我们想到了,将代码封装,简化了使用,减少了冗余 javascript ajax代码如下: var x ...
- WebApp开发:ajax请求跨域问题的解决
服务端:PHP 客户端:Andorid, HTML5, jQuery, ajax 现象:本想通过jQuery的ajax功能从服务器取回数据存到手机的缓存里,结果总是错误,后来想到可能是跨域问题,所以查 ...
- 根据WSDL生成代理类方式
方式一: 1.使用VS2010提供的工具wsdl.exe由WSDL文件生成cs文件 使用wsdl.exe的/serverInterface选项(或缩写的 /si)指定输入的wsdl文件(注意,如果要转 ...
- 在Eclipse中搭建C/C++环境
在Eclipse中搭建C/C++环境 本文地址:http://blog.csdn.net/you_and_me12/article/details/7389934 习惯了使用eclipse编程后,现在 ...
- 重燃你的PHP安全分析之火
关于脚本安全这个话题好像永远没完没了,如果你经常到国外的各种各样的bugtraq上,你会发现有一半以上都和脚本相关,诸如SQL injection,XSS,Path Disclosure,Remote ...
- hdu 4111 Alice and Bob
组合游戏题: 组合游戏的规则: 1.必败态的所有后继都是必胜态: 2.必胜态最少有一个必败的后继: 这里的必胜态是f[1][0][0][0]; 其中f[a][b][c][d]表示有a个1,b个2,c个 ...
- Java使用JAX-WS来写webservice时 Unable to create JAXBContext
webservice,作为web开发人员来说必须掌握的一门技术,它的好处这里就不多说了,eclipse中自带了一种生成webservice的 方法,使用JAX-WS,如果我没有弄错的话,它需要java ...
- Qt创建窗体的过程
版权声明 本文为原创作品,请尊重作者的劳动成果.转载必须保持文章完整性,并以超链接形式注明原始作者“ tingsking18”和 主站点地址,方便其他朋友提问和指正. QT源码解析(一) QT创建窗口 ...