TextView and EditView show image
来源:互联网 发布:dede源码下载 编辑:程序博客网 时间:2024/05/16 16:18
TextViews are one of the basis of Android UI framework. Buttons, EditTexts, RadioButtons are, indeed, TextViews. And they are also one of the most powerful tools we have to create our layouts. Knowing them in deep can help us save time, views and simplify our layouts, making our UI faster.
In this series, I will talk about TextView and its direct subclasses. I’m beginning with one of the most basic cases, that you may know. But If you don’t, you will love it.
There are a lot of situations where we need to use a TextView (or EditText, Button… remember they are subclasses) with a simple image at the left or right. That’s not that difficult, right? We simply need an ImageView plus a TextView inside a LinearLayout:
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_marginTop
=
"16dp"
android:orientation
=
"horizontal"
android:gravity
=
"center"
android:layout_gravity
=
"center_horizontal"
>
<
TextView
android:text
=
"@string/my_contacts"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
<
ImageView
android:paddingLeft
=
"8dp"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:src
=
"@drawable/ic_action_add_group"
/>
</
LinearLayout
>
Lint already warns that there is a better way to do this by using a TextView and a compound drawable. What’s this? Our TextView has four properties that let us specify images to be set around it. These ones are: drawableLeft, drawableRight, drawableTop and drawableBottom. We also can use another one which defines the padding among the text and the images: drawablePadding.
Ok then, let’s change the previous layout and get rid of LinearLayout and ImageView:
<
TextView
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_marginTop
=
"16dp"
android:gravity
=
"center"
android:layout_gravity
=
"center_horizontal"
android:text
=
"@string/my_contacts"
android:drawableRight
=
"@drawable/ic_action_add_group"
android:drawablePadding
=
"8dp"
/>
Cool! We removed a level of complexity and made our XML much simpler. It’s almost as easy to set these drawables from code:
textView.setCompoundDrawablesWithIntrinsicBounds(
0
,
0
, R.drawable.ic_action_add_group,
0
);
textView.setCompoundDrawablePadding(...);
With this idea in mind, we can create very interesting layouts. Let’s create a simple username and password form:
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"250dp"
android:layout_height
=
"wrap_content"
android:layout_gravity
=
"center_horizontal"
android:gravity
=
"center"
android:orientation
=
"vertical"
android:layout_marginTop
=
"16dp"
>
<
EditText
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:gravity
=
"center_vertical"
android:hint
=
"@string/user_name"
android:drawableLeft
=
"@drawable/ic_action_person"
android:drawablePadding
=
"8dp"
/>
<
EditText
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:gravity
=
"center_vertical"
android:hint
=
"@string/password"
android:drawableLeft
=
"@drawable/ic_action_accounts"
android:drawablePadding
=
"8dp"
/>
<
Button
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/log_in"
android:drawableRight
=
"@drawable/ic_action_accept"
/>
</
LinearLayout
>
Quite easy. Next episode will cover a way to make a drawable clickable, another trick to keep your layout simple by taking the most out of TextViews.
- TextView and EditView show image
- image to DataBase and show the image
- image captioning-Show and Tell: A Neural Image Caption Generator
- Android TextView/EditView
- TextView和EditView使用
- TextView与EditView
- read a image and show it's channel
- 读书笔记--《Show and Tell: A Neural Image Caption Generator》
- android TextView EditView 属性收集
- 使用TextView和EditView控件
- TextView EditView ImageView常用属性
- Android TextView show Html
- Image Show by SWT
- Show image in matplotlib
- Android 控件之TextView、EditView、CheckBox
- textview button editview以及绑定监听器
- 第一篇Activity切换,TextView和EditView
- Android学习(3)TextView和EditView
- oracle 导入导出数据
- 《程序员的第一年》---------- 选择转行了
- [原]VC++ URL编码的解码
- 采用Latex的moderncv制作简历
- 面试准备汇总
- TextView and EditView show image
- OpenCV2.4.4中调用SIFT特征检测器进行图像匹配
- eval使用
- COCOSBuilder 源码, cocos2d 和cocos2dx混编
- 架构设计的抉择
- Windows XP 与 Windows 2000 句柄表(增加个人注解)
- JAVA之异常处理问题
- 上传文件到服务器
- linux下常用拷贝命令