如何建网站的步骤,网站规划的解释,wordpress 无效用户名,做网站不错的公司Android学习之路(1) 文本
一、设置文本内容
设置文本内容的两种方式#xff1a;
一种是在XML文件中通过属性android:text设置文本代码如下
TextViewandroid:idid/tv_helloandroid:layout_widthwrap_contentandroid:layout_heightwrap_c…Android学习之路(1) 文本
一、设置文本内容
设置文本内容的两种方式
一种是在XML文件中通过属性android:text设置文本代码如下
TextViewandroid:idid/tv_helloandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text你好世界 /另一种是在Java代码中调用文本视图对象的setText方法设置文本代码如下
// 获取名为tv_hello的文本视图TextView tv_hello findViewById(R.id.tv_hello);tv_hello.setText(你好世界); // 设置tv_hello的文字内容在Java文件中新建一个类名为TextViewActivity的Java文件。调用文本视图后如下图所示 在XML文件中设置文本的话
在布局文件 res/layout)创建activity_text_view 把鼠标移到“你好世界”上方时Android Studio会弹出如下图所示的提示框。 看到提示内容为“Hardcoded string “你好世界”, should use string resouce”意思说这几个字是硬编码的字符串建议使用来自string的资源。原来Android Studio不推荐在XML布局文件里直接写字符串因为可能有好几个页面都显示“你好世界”若想把这句话换成“你吃饭了吗”就得一个一个XML文件改过去无疑费时费力。故而Android Studio推荐把字符串放到专门的地方管理这个名为string的地方位于res/values目录下的strings.xml打开该文件发现它的初始内容如下所示 看来strings.xml定义了一个名为“app_name”的字符串常量其值为“chapter03”。那么在此添加新的字符串定义字符串名为“hello”字符串值为“你好世界”添加之后的strings.xml内容如下所示 添加完新的字符串定义回到XML布局文件将android:text属性值改为“string/字符串名”这般也就是“string/hello”修改之后的TextView标签示例如下
TextViewandroid:idid/tv_helloandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textstring/hello /整个页面浏览如下 然后把鼠标移到“你好世界”上方此时Android Studio不再弹出任何提示了。若要在Java代码中引用字符串资源则调用setText方法时填写形如“R.string.字符串名”的参数就本例而言填入“R.string.hello”修改之后的Java代码示例如下
// 获取名为tv_hello的文本视图
TextView tv_hello findViewById(R.id.tv_hello);
tv_hello.setText(R.string.hello); // 设置tv_hello的文字内容至此不管XML文件还是Java代码都从strings.xml引用字符串资源以后想把“你好世界”改为其他文字的话只需改动strings.xml一个地方即可。
最后我们看一下res/values目录下的strings.xml 布局文件 res/layout下的activity_text_view) Java代码如下; 运行前记得有一个地方一定要注意 完成后我们可以运行一下看看结果运行结果虚拟机图像如下 二、设置文本的大小
TextView允许设置文本内容也允许设置文本大小在Java代码中调用setTextSize方法即可指定文本 大小就像以下代码这样
// 从布局文件中获取名叫tv_sp的文本视图TextView tv_sp findViewById(R.id.tv_sp);tv_sp.setTextSize(30); // 设置tv_sp的文本大小这里的大小数值越大则看到的文本也越大大小数值越小则看到的文本也越小。在XML文件中则通 过属性android:textSize指定文本大小可是如果给TextView标签加“android:textSize“30””数字马 上变成红色如下图所示鼠标移过去还会提示错误“Cannot resolve symbol ‘30’”意思是无法解析“30”这个符号。 原来文本大小存在不同的字号单位XML文件要求在字号数字后面写明单位类型常见的字号单位主要 有px、dp、sp 3种分别介绍如下。
1px
px是手机屏幕的最小显示单位它与设备的显示屏有关。一般来说同样尺寸的屏幕比如6英寸手 机如果看起来越清晰则表示像素密度越高以px计量的分辨率也越大。
2dp
dp有时也写作dip指的是与设备无关的显示单位它只与屏幕的尺寸有关。一般来说同样尺寸的屏 幕以dp计量的分辨率是相同的比如同样是6英寸手机无论它由哪个厂家生产其分辨率换算成dp单 位都是一个大小。
3sp
sp的原理跟dp差不多但它专门用来设置字体大小。手机在系统设置里可以调整字体的大小小、标 准、大、超大。设置普通字体时同数值dp和sp的文字看起来一样大如果设置为大字体用dp设置 的文字没有变化用sp设置的文字就变大了。
字体大小采用不同单位的话显示的文字大小各不相同。例如30px、30dp、30sp这3个字号在不同 手机上的显示大小有所差异。有的手机像素密度较低一个dp相当于两个px此时30px等同于15dp 有的手机像素密度较高一个dp相当于3个px此时30px等同于10dp。假设某个App的内部文本使用字 号30px则该App安装到前一部手机的字体大小为15dp安装到后一部手机的字体大小为10dp显然 后一部手机显示的文本会更小。
至于dp与sp之间的区别可通过以下实验加以观察。首先创建测试活动页面该页面的XML文件分别声 明30px、30dp、30sp这3个字号的TextView控件布局内容如下所示
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:padding5dpandroid:orientationverticalTextViewandroid:idid/tv_pxandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text你好世界px大小android:textSize30px /TextViewandroid:idid/tv_dpandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text你好世界dp大小android:textSize30dp /TextViewandroid:idid/tv_spandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text你好世界sp大小android:textSize30sp /
/LinearLayout接着打开手机的设置菜单依次选择“显示”→“字体与显示大小”确认当前的字体为标准大小如下图所示。然后在手机上运行测试App。
发现字号单位30px和30dp的文字大小不变而30sp的文字随着系统字体一起变大了。既然XML文件要求android:textSize必须指定字号单位为什么Java代码调用setTextSize只填数字不填单位呢其实查看SDK源码找到setTextSize方法的实现代码如下所示
public void setTextSize(float size) { setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}原来纯数字的setTextSize方法内部默认字号单位为spCOMPLEX_UNIT_SP这也从侧面印证了之前的说法sp才是Android推荐的字号单位。 计算规则
我们以一个 4.95 英寸 1920 * 1080 的 nexus5 手机设备为例
Dpi : 计算直角边像素数量 19202108022202^2勾股定理。 计算 DPI2202 / 4.95 445。得到这个设备的 DPI 为 445 每英寸的距离中有 445 个像素
Density 上面得到每英寸中有 445 像素那么 density 为每平方英寸中的像素数量应该为 445^2198025。 Dip 所有显示到屏幕上的图像都是以 px 为单位Dip 是我们开发中使用的长度单位最后他也需要转换成 px计算这个设备上 1dip 等于多少 px px dip x dpi /160 根据换算关系 320 x 480分辨率3.6寸的手机dpi为1601dp1px 实验一
相同分辨率不同大小的手机AB 假如AB都设置一个宽度为100dp的TextView 得出结论
对于相同分辨率的手机屏幕越大同DP的组件占用屏幕比例越小。
实验二
相同大小不同分辨率的手机AB 假如AB都设置一个宽度为100dp的TextView 得出结论
对于相同尺寸的手机即使分辨率不同同DP的组件占用屏幕比例也相同。
综上
dp的UI效果只在相同尺寸的屏幕上相同如果屏幕尺寸差异过大则需要重做dp适配
这也是平板需要单独做适配的原因可见dp不是比例。
完整代码如下
layout
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:gravitycenterandroid:orientationverticalTextViewandroid:idid/tv_pxandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textstring/helloandroid:textSize30px/TextViewandroid:idid/tv_dpandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textstring/helloandroid:textSize30dp/TextViewandroid:idid/tv_spandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textstring/helloandroid:textSize30sp//LinearLayoutJava文件:
package com.example.chapter03;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.widget.TextView;public class TextSizeActivity extends AppCompatActivity {Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_text_size);/* TextView tv_hello findViewById(R.id.tv_hello);tv_hello.setTextSize(30);*/}
}values/string:
resourcesstring nameapp_namechapter03/stringstring namehello你好世界/string
/resourcesAndroidManifest:
?xml version1.0 encodingutf-8?
manifest xmlns:androidhttp://schemas.android.com/apk/res/androidpackagecom.example.chapter03applicationandroid:allowBackuptrueandroid:iconmipmap/ic_launcherandroid:labelstring/app_nameandroid:roundIconmipmap/ic_launcher_roundandroid:supportsRtltrueandroid:themestyle/Theme.MyApplicationactivityandroid:name.TextSizeActivityandroid:exportedtrueintent-filteraction android:nameandroid.intent.action.MAIN /category android:nameandroid.intent.category.LAUNCHER //intent-filter/activity/application/manifest三、设置文本的颜色
除了设置文字大小文字颜色也经常需要修改毕竟Android默认的灰色文字不够醒目。在Java代码中调 用setTextColor方法即可设置文本颜色具体在Color类中定义了12种颜色详细的取值说明见下表 比如以下代码便将文本视图的文字颜色改成了绿色
// 从布局文件中获取名为tv_code_system的文本视图TextView tv_code_system findViewById(R.id.tv_code_system);// 将tv_code_system的文字颜色设置系统自带的绿色tv_code_system.setTextColor(Color.GREEN);可是XML文件无法引用Color类的颜色常量为此Android制定了一套规范的编码标准将色值交由透明 度alpha和RGB三原色红色red、绿色green、蓝色blue联合定义。该标准又有八位十六进制数与六 位十六进制数两种表达方式例如八位编码FFEEDDCC中FF表示透明度EE表示红色的浓度DD表示 绿色的浓度CC表示蓝色的浓度。透明度为FF表示完全不透明为00表示完全透明。RGB三色的数值越 大表示颜色越浓也就越暗数值越小表示颜色越淡也就越亮。RGB亮到极致就是白色暗到极 致就是黑色。
至于六位十六进制编码则有两种情况它在XML文件中默认不透明等价于透明度为FF而在代码 中默认透明等价于透明度为00。以下代码给两个文本视图分别设置六位色值与八位色值注意添加0x前缀表示十六进制数
// 从布局文件中获取名为tv_code_six的文本视图TextView tv_code_six findViewById(R.id.tv_code_six);// 将tv_code_six的文字颜色设置为透明的绿色透明就是看不到tv_code_six.setTextColor(0x00ff00);// 从布局文件中获取名为tv_code_eight的文本视图TextView tv_code_eight findViewById(R.id.tv_code_eight);// 将tv_code_eight的文字颜色设置为不透明的绿色即正常的绿色tv_code_eight.setTextColor(0xff00ff00);运行测试App发现tv_code_six控件的文本不见了其实是变透明了而tv_code_eight控件的文本显 示正常的绿色。 在XML文件中可通过属性android:textColor设置文字颜色但要给色值添加井号前缀“#”设定好文本颜 色的TextView标签示例如下完整代码见下文
TextViewandroid:idid/tv_xmlandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text布局文件设置六位文字颜色android:textColor#00ff00android:textSize17sp /就像字符串资源那样Android把颜色也当作一种资源打开res/values目录下的colors.xml发现里面 已经定义了3种颜色
resourcescolor namecolorPrimary#008577/colorcolor namecolorPrimaryDark#00574B/colorcolor namecolorAccent#D81B60/color
/resources那么先在resources节点内部补充如下的绿色常量定义
color namegreen#00ff00/color然后回到XML布局文件把android:textColor的属性值改为“color/颜色名称”也就是android:textColor“color/green”修改之后的标签TextView如下所示
TextViewandroid:idid/tv_valuesandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text资源文件引用六位文字颜色android:textColorcolor/greenandroid:textSize17sp /不仅文字颜色还有背景颜色也会用到上述的色值定义在XML文件中通过属android:background设 置控件的背景颜色。Java代码则有两种方式设置背景颜色倘若色值来源于Color类或十六进制数则调 用setBackgroundColor方法设置背景倘若色值来源于colors.xml中的颜色资源则调用setBackgroundResource方法以“R.color.颜色名称”的格式设置背景。下面是两种方式的背景设定代码 例子
// 从布局文件中获取名叫tv_code_background的文本视图TextView tv_code_background findViewById(R.id.tv_code_background);// 将tv_code_background的背景颜色设置为绿色tv_code_background.setBackgroundColor(Color.GREEN); // 在代码中定义的色值tv_code_background.setBackgroundResource(R.color.green); // 颜色来源于资源文件注意属性android:background和setBackgroundResource方法它俩用来设置控件的背景不单单是 背景颜色还包括背景图片。在设置背景图片之前先将图片文件放到res/drawable***目录以drawable开头的目录不仅仅是drawable目录然后把android:background的属性值改为“drawable/不含扩展名的图片名称”或者调用setBackgroundResource方法填入“R.drawable.不含扩 展名的图片名称”
完整代码如下
layout\activity_text_color.xml
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:gravitycenterandroid:orientationverticalTextViewandroid:idid/tv_code_systemandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text代码设置系统自带的颜色android:textSize17sp/TextViewandroid:idid/tv_code_eightandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text代码设置八位文字颜色android:textSize17sp/TextViewandroid:idid/tv_code_sixandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text代码设置六位文字颜色android:textSize17sp/TextViewandroid:idid/tv_code_xmlandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text布局文件设置六位文字颜色android:textSize17spandroid:textColor#00ff00/TextViewandroid:idid/tv_valuesandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text资源文件引用六位文字颜色android:textSize17spandroid:textColorcolor/green/TextViewandroid:idid/tv_code_backgroundandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text背景设置为绿色android:textSize17sp/!-- android:textColorcolor/green --
/LinearLayoutlayout\activity_text_color.xml的视图 TextColorActivity.java
package com.example.chapter03;import androidx.appcompat.app.AppCompatActivity;import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;public class TextColorActivity extends AppCompatActivity {Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_text_color);//从布局文件中获取名叫tv_code_system的文本视图TextView tv_code_system findViewById(R.id.tv_code_system);//将tv_code_system的文本颜色设置系统自带的绿色tv_code_system.setTextColor(Color.GREEN);//从布局文件中获取名叫tv_code_eight的文本视图TextView tv_code_eight findViewById(R.id.tv_code_eight);//将tv_code_eight的文件颜色设置为不透明的绿色即正常的绿色tv_code_eight.setTextColor(0xff00ff00);//从布局文件中获取名叫tv_code_six的文本视图TextView tv_code_six findViewById(R.id.tv_code_six);//将tv_code_eight的文件颜色设置为透明的绿色,透明就是看不到tv_code_six.setTextColor(0x00ff00);//从布局文件中获取名叫tv_code_background的文本视图TextView tv_code_background findViewById(R.id.tv_code_background);//将tv_code_background的背景颜色设置绿色tv_code_background.setBackgroundColor(Color.GREEN);//tv_code_background.setTextColor(R.color.green);}
}values/colors.xml
?xml version1.0 encodingutf-8?
resourcescolor namepurple_200#FFBB86FC/colorcolor namepurple_500#FF6200EE/colorcolor namepurple_700#FF3700B3/colorcolor nameteal_200#FF03DAC5/colorcolor nameteal_700#FF018786/colorcolor nameblack#FF000000/colorcolor namewhite#FFFFFFFF/colorcolor namegreen#00ff00/color
/resources虚拟机运行结果