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网页中常见的 ...
随机推荐
- Central Europe Regional Contest 2012 Problem c: Chemist’s vows
字符串处理的题目: 学习了一下string类的一些用法: 这个代码花的时间很长,其实可以更加优化: 代码: #include<iostream> #include<string> ...
- 【BZOJ 1597】 [Usaco2008 Mar]土地购买 (斜率优化)
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3601 Solved: 1322 Descrip ...
- java中的单例模式与doublecheck
转自: http://devbean.blog.51cto.com/448512/203501 在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容易出现问题.下面就单 ...
- Yii处理流程
Yii的应用程序处理流程 用户访问URL http://www.example.com/blog/index.php?r=site/contact 1.入口脚本被网站服务器执行以处理此请求. 2.一个 ...
- linux使用man命令后退出
linux使用man命令后 使用q退出
- hadoop2.2编程:DFS API 操作
1. Reading data from a hadoop URL 说明:想要让java从hadoop的dfs里读取数据,则java 必须能够识别hadoop hdfs URL schema, 因此我 ...
- POJ_3666_Making_the_Grade_(动态规划)
描述 http://poj.org/problem?id=3666 给一串坡的高度,现在要调整某些点,使整个坡单调不降或单调不升.调整的花费为原高度与先高度的差的绝对值,问最小花费(可单增可单降). ...
- ☀【CSS3】icon
Navicon Transformicons: Animated Navigation Icons with CSS Transformshttp://sarasoueidan.com/blog/na ...
- 【转】你应该知道的十个VirtualBox技巧与高级特性
原文网址:http://www.searchvirtual.com.cn/showcontent_76463.htm VirtualBox集成的许多功能你可能从来没有使用过,即使你经常用它来运行虚拟机 ...
- Android-adb shell 读取手机系统文件
1.首先保证手机是root 状态 2.运行 adb shell 页面以后 su root 3.ls 就会发现目录结构可以显示了