您的位置:首页 > 移动开发 > Android开发

Textview相关知识

2015-04-29 20:12 323 查看
1. TextView内容的初始化:

所谓内容初始化,指的是Text显示的内容。

1.1: 在Layout/main.xml中初始化:

 <TextView

        android:id="@+id/TV0"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />

    

    <TextView

        android:id="@+id/myTextView01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/text01"

        android:textColor="@drawable/white"

        android:background="@drawable/black"


        android:layout_below="@+id/TV0"       

                />

可以看到TextView的内容,颜色,背景色,字体等。都可以在Layout中说明,程序启动后会相应的对象会被创建。 

1.2: 在代码中初始化内容:

private TextView myTestTextView;

通常Activity的onCreate()中,可以修改或者初始化TextView内容,颜色,背景色等。(但在Fragment模式下不是,见注1)

myTestTextView = (TextView)findViewById(R.id.myTextView01);

myTestTextView.setBackgroundColor(Color.WHITE);
myTestTextView.setTextColor(Color.BLACK);

myTestTextView.setText(R.string.text02);

上面的例子是使用Android.graphics.Color的静态变量。 也可以使用自定义的颜色值。

myTestTextView = (TextView)findViewById(R.id.myTextView01);

        

Resources resources = getResources();

int color = resources.getColor(R.drawable.black);

myTestTextView.setBackgroundResource(R.drawable.white);

myTestTextView.setTextColor(color);

        

 myTestTextView.setText(R.string.text02);

注意:虽然TextView的字体颜色和背景色都可以采用自定义的颜色。但方法稍有不同。

setBackgroundResource()用来设置背景色。这个比老方法更推荐。

老方法是:

Resources resources = getResources();


Drawable drawable = resource.getDrawable(R.drawable.white);

myTestTextView.setBackgroundDrawable(R.drawable.white);

2. String常量和Color常量的预定义: 

2.1: Color常量的定义:

Android开发中,需要用到颜色的地方不少,可以实现将颜色定义好,存放在res/values中。

方法如下:

光标选中 values,  File->New->Other->Android XML Values File

注意: Root_Element: 缺省就是:resource.

输入文件名:color.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>


    <drawable name="darkgray">#808080FF</drawable>

      <drawable name="white">#FFFFFFFF</drawable>

      <drawable name="ivory">#FFFFF0</drawable>

    <drawable name="lightyellow">#FFFFE0</drawable>

    <drawable name="yellow">#FFFF00</drawable>

    <drawable name="snow">#FFFAFA</drawable>

    <drawable name="floralwhite">#FFFAF0</drawable>

    <drawable name="lemonchiffon">#FFFACD</drawable>

    <drawable name="cornsilk">#FFF8DC</drawable>

    <drawable name="seashell">#FFF5EE</drawable>

    <drawable name="lavenderblush">#FFF0F5</drawable>

    <drawable name="papayawhip">#FFEFD5</drawable>

    <drawable name="blanchedalmond">#FFEBCD</drawable>

    <drawable name="mistyrose">#FFE4E1</drawable>

    <drawable name="bisque">#FFE4C4</drawable>

    <drawable name="moccasin">#FFE4B5</drawable>

    <drawable name="black">#000000</drawable>
</resources>

2.2:String常量的定义:

Android开发中,可以需要使用的String常量定义好,存放在res/values中。

创建方法和color.xml相同。命名为:strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="app_name">TestTextView</string>

    <string name="hello_world">Hello world!</string>

    <string name="action_settings">Settings</string>

    <string name="text01">blog.sina.com.cn/samzhen1977. String1</string>

    <string name="text02">SamInfo: This is sam\'s Message. String2</string>

</resources>

3. 从strings.xml, color.xml中得到具体的String和Color

在Anrdroid程序中,时常会需要取出color.xml中定义的颜色,strings.xml中定义的常量。

Color:

Resource resource = getResource();

int color = resource.getColor(R.drawable.white);

String:

String str = getString(R.string.str_1);

注1:

activity_main.xml , fragment_main.xml 相关信息。

在fragment_main.xml中创建的TextView, 不能在ActionBarActivity的onCreate中使用,因为此时还未载入frament_main.xml. 所以对象未创建。直到onCreateView()后,frament_main.xml中的TextView才被真正创建。

所以,可以在onStart()中使用TextView。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android TextView