第四天 文本显示组件(TextView)

来源:互联网 发布:慢走丝统赢编程 编辑:程序博客网 时间:2024/05/04 05:02

android.widget.TextView 类是View类的直接子类,所以在本组件之中也会提供更多配置的相关属性。


<TextViewandroid:id="@+id/textView" -----定义组件的IDandroid:layout_width="fill_parent" ------组件宽度为屏幕宽度android:layout_height="wrap_content" ------组件高度为文字高度android:textColor="#FFFF00" -----文字颜色为黄色android:textSize="25dp" -----文字大小为25dpandroid:text="@string/hello_world"/> -------设置显示文字


px与dp(dip)之间的区别

在xml布局文件中,我们既可以设置px,也可以设置dp(或者dip)。一般情况下,我们都会选择使用dp,这样可以保证不同屏幕分辨率的机器上布局一致。

下面是关于px与dp之间的转换代码

import android.content.Context;    public class DensityUtil {        /**      * 根据手机的分辨率从 dp 的单位 转成为 px(像素)      */      public static int dip2px(Context context, float dpValue) {          final float scale = context.getResources().getDisplayMetrics().density;          return (int) (dpValue * scale + 0.5f);      }        /**      * 根据手机的分辨率从 px(像素) 的单位 转成为 dp      */      public static int px2dip(Context context, float pxValue) {          final float scale = context.getResources().getDisplayMetrics().density;          return (int) (pxValue / scale + 0.5f);      }  }  

定义样式表在res目录下的values中新建一个样式 style.xml

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android">    <style name="my_style">        <item name="android:textSize">25dp</item><span style="white-space:pre"></span>----字体        <item name="android:textColor">#FFFF00</item><span style="white-space:pre"></span>----颜色        <item name="android:autoLink">all</item><span style="white-space:pre"></span>----定义链接显示文字<item name="android:layout_width">fill_parent</item><item name="android:layout_height">wrap_content</item><item name="android:layout_marginTop>40dp</item>    </style></resources>


然后在TextView定义中引用此样式表
<TextView                style="@style/msg_style"<span style="white-space:pre"></span>----定义组件显示风格        android:id="@+id/textView2"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:text="www.xxxxx.com" />

之后就可以用样式表文件进行统一的属性配置。


因为Button和EditText都是TextView的子类,所以只要对TextView有足够的了解,就可以迅速掌握Button与EditText.所以不在此介绍什么了。


0 0