TextView属性以及R.*系列『Android系列五』

        本篇涉及:改变文本文字setText()多态方法、更改背景色setBackgroundColor()和文本文字颜色setTextColor()、文字大小setTextSize()、指定文字字体。


        首先看下有关文件的代码,layout/main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/firstText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

        values/strings.xml

<resources>


    <string name="app_name">FirstBase</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">Main</string>
    <string name="tryfirst">why?</string>
    <string name="tryagain">whats happened?</string>


</resources>

        src里面的java源文件:

package com.dy.study.firstbase;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.widget.TextView;

public class Main extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		findViews();	//集中声明控件对象方法

		aboutText();	//有关TextView控件的变动
	}

	private TextView firstText;

	private void findViews() {
		firstText = (TextView) findViewById(R.id.firstText);
	}

	private void aboutText() {
		firstText.setText(R.string.tryfirst); //修改文本文字内容为常量tryfirst的内容
		firstText.setBackgroundColor(Color.GREEN); //修改背景颜色为green
		firstText.setTextColor(Color.RED); //修改字体颜色为red
	}
}

        在Main.java的aboutText()方法里面修改了三项,结果如下:

TextView属性以及R.*系列『Android系列五』_第1张图片


        这里需要说明的是:

        1.main.xml中的android:id="@+id/firstText",这个是给控件起个名字,以后好调用它。

        2.main.java中的findViewById(R.id.firstText),这个就是调用某个名字的控件。


        有关setText的多态方法如下:

public final void setText(CharSequence text);
public final void setText(int resId);
public void setText(CharSequence text,TextView.BufferType type);
public final void setText(int resId,TextView.BufferType type);
public final void setText(char[] text,int start,int len);

        然后继续看怎么改变文字的大小,看到提示里面的参数是float类型的,直接使用setTextSize(18),这时候问题出现了,我们知道平时用的字体大概在12到14之间,这个18感觉大小不太对?于是想要知道这个18的单位是什么,网络查了下,发现setTextSize里面的单位是sp(Scaled Pixels放大像素),一般用于字体显示,而我们平时常用的是dp(设备独立像素)和px(像素),具体这几种单位什么关系,篇幅比较长,而且和本文主题偏离比较大,还是以后另开篇幅处理,这里想要改变字体大小,多用几个数字试试,达到满意为止吧。

        指定文字字体:setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Something.ttf"));这里需要注意的是,fonts文件夹新建在assets文件夹下面,Something.ttf字体文件放在fonts文件夹下面,而且必须符合Ture Type Font格式。


        还有R.string.*和R.id.*甚至还有R.color.*、R.layout.*等等以及Color.GREEN:

        R.id.*是获取某个控件的ID值,R.layout.*是获取某个页面,R.string.*是获取values文件夹下某个xml文件中节点为string的某个常量,R.color.*是获取values文件夹下xml文件中节点为color的某个常量,另外还有R.drawable.*等。Color.GREEN、Color.RED为设定好的颜色,意思就很简单啦。


        另外,有时候调用某些方法的时候提示出现“call requires API level 16(current min is 8)”时,说明这个方法不适合于低版本,在父方法体前加上@SuppressLint("NewApi"),或者在AndroidManifest.xml中改变android:minSdkVersion为16也可以。

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />


你可能感兴趣的:(android,String,layout,Class,tools,menu)