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网页中常见的 ...
随机推荐
- Android中的pix,sp,dp相关概念
px( pixel) 像素,可以简单的理解为一个点或方块,用以颜色的显示(单位),一般指印刷品或屏幕设置设备的颜色显示定义. dip(device independent pixels)设备独立像素. ...
- cocos2dx3.4 导出节点树到XML文件
l利用cocostudio做UI和场景时,经常要去获取某个节点,cocostudio2.1开始加入了文件的概念,可以创建场景,节点,层等文件,把公用的东西创建到文件里,然后把这个文件拖到场景里使用,达 ...
- 传感器- 加速计 - CoreMotion
/** * CoreMotion * */ #import "ViewController.h" #import <CoreMotion/CoreMotion.h> ...
- 守望先锋overwatch美服外服设置方法
打开:C:\Users\你的用户名\AppData\Roaming\Battle.net\Battle.net.config 替换为下方内容: { "Client": { &quo ...
- XMLHttpRequest2的进步之处
本文参考自:XMLHttpRequest2 新技巧 (重点保留demo,方便自己日后查阅) HTML5是现在web开发中的热点,虽然关于web app和local app一直有争论,但是从技术学习的角 ...
- GITLAB的版本回退(非命令行)
今天遇到小韩的问题,大约解决如下:
- 使用SparseArray代替HashMap
HashMap是java里比较常用的一个集合类,我比较习惯用来缓存一些处理后的结果.最近在做一个Android项目,在代码中定义这样一个变量,实例化时,Eclipse却给出了一个 performanc ...
- bzoj1558
好题,初看以为只要差分然后维护相同的段数目但是请注意下面的情况2 3 5 8 9 1 2 3 4 这显然答案是3而不是4因此我们还要再维护ld,rd表示左右单独的段长度和s表示不包括左右单独的段,中间 ...
- bzoj2426
稍微列个式子就知道是贪心 ..] of longint; m,b,h0,n,i,p,j,x,ans,s:longint; procedure swap(var a,b:longint); var c: ...
- HDU-1540 Tunnel Warfare
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...