第二章 吸引你的眼球—UI编程(6)
2015-01-04 09:25
190 查看
2.2 彰显你的个性—自定义UI组件
很多时候,Android的常用控件并不能满足我们的需求。为了吸引更多的眼球,达到标新立异的效果,我们可以自己来定义各种控件。我们可以通过继承基础控件来重写某些环节,当然我们也可以将多个控件组合成一个新控件来使用。我们先来看看下面一个例子,在这个例子当中,我们实现了一个带有图片和文字的按钮。
首先,定义一个layout,实现按钮内部的布局。代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv" android:paddingTop="5dip" android:paddingBottom="5dip" android:paddingLeft="20dip" android:layout_gravity="center_vertical" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#333" android:id="@+id/tv" android:layout_marginLeft="8dip" android:layout_gravity="center_vertical" /> </LinearLayout> |
// import略 public class MyLayout extends LinearLayout { private ImageView iv; private TextView tv; public MyLayout(Context context) { this(context, null); } public MyLayout(Context context, AttributeSet attrs) { super(context, attrs); // 导入布局 LayoutInflater.from(context).inflate(R.layout.mylayout, this, true); iv = (ImageView) findViewById(R.id.iv); tv = (TextView) findViewById(R.id.tv); } /** * 设置图片资源 */ public void setImageResource(int resId) { iv.setImageResource(resId); } /** * 设置显示的文字 */ public void setTextViewText(String text) { tv.setText(text); } } |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.char2.MyLayout android:id="@+id/my_button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/button_gray"/> </LinearLayout> |
MyLayout myLayout = (MyLayout)findViewById(R.id.my_button); myLayout.setImageResource(R.drawable.close); myLayout.setTextViewText("关闭"); |
图2-18 多个控件的组合 --------------------------------------------试着放个广告 现在没工作 要生存 没办法平安陆金所 隶属于平安集团的p2p平台年投资回报率7%-9% 是替代银行理财的首选个人经验教训 推荐投资安鑫或者有担保的彩虹项目不要投资安e 那个几乎无法转让 想提前提现非常困难注册链接 http://affiliate.lufax.com/action/36XBU用此链接注册 你我都会有几十元的额外现金奖励--------------------------------------------
这样,一个带文字和图片的组合按钮控件就完成了。这样梳理一下,使用还是非常简单的。下面,我们再来看一个例子,自定义一个控件,显示带有边框的字。我们新建一个类继承TextView,然后重写它的onDraw方法,部分代码如下:
private Canvas canvas = new Canvas(); public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); this.canvas = canvas; Rect rec = canvas.getClipBounds(); Paint paint = new Paint(); paint.setColor(Color.WHITE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(2); canvas.drawRect(rec, paint); } |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.char2.MyTextView android:layout_centerInParent="true" android:id="@+id/my_button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="自定义控件" android:textSize="24sp"/> </RelativeLayout> |
图2-19 重写控件onDraw方法 可以看到,带有边框的字已经实现了。
相关文章推荐
- 第二章 吸引你的眼球—UI编程(4)
- 第二章 吸引你的眼球—UI编程(7)
- 第二章 吸引你的眼球—UI编程(5)
- 第二章 吸引你的眼球—UI编程(8)
- 第二章 吸引你的眼球—UI编程(1)
- 第二章 吸引你的眼球—UI编程(2)
- 第二章 吸引你的眼球—UI编程(3)
- 优秀网页设计:35个吸引眼球的精美作品集网站
- 优秀网页设计:35个吸引眼球的精美作品集网站
- 推荐25个吸引眼球的创意界面设计
- 分享20个吸引眼球的高品质免费PSD网站模板
- 时尚设计风格最吸引眼球
- 25个吸引眼球的广告设计
- [英语阅读]荷兰女子“高跟鞋赛跑”吸引眼球
- 好的创意才够吸引眼球,同时也很赚大钱,你拥有好创意吗
- 25个吸引眼球的广告设计
- 棋牌游戏开发独具创意吸引用户的“眼球”
- 游戏大厅 从基础开始(3)——最吸引眼球的部分 客户端与服务器的连接
- 12款吸引眼球的免费英文涂鸦艺术字体下载
- 日公司美女大腿上做广告吸引眼球