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

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


<TextView
android:id="@+id/textView"	 -----定义组件的ID
android:layout_width="fill_parent"	 ------组件宽度为屏幕宽度
android:layout_height="wrap_content"	 ------组件高度为文字高度
android:textColor="#FFFF00"	 -----文字颜色为黄色
android:textSize="25dp"	 -----文字大小为25dp
android: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值