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

android基础-TextView

2014-03-11 16:56 239 查看
对于android开发本人还没写过什么代码,只是把别人的博客给看了下,先大体感觉一样!所以怎么说呢!有点感觉啦!所以该动手啦!呵呵。。。

总的来说对于TextView我的第一感觉就像是Qt中的Qlabel呵呵。。。这样有比较的学习感觉快多啦!主要是非常好理解!对于QLabel中我们第一个用的就是setText()这个功能,所以在这里我对TextView也是先用这个吧!ok!开始啦!

由于android开发的布局可以使用xml文件来开发,所以不是所有的都是要用code来完成的,对于定死了的,xml布局是挺有用的,当然对于要可变动的话,就要再说啦!

ok!废话不多说!开始吧!

1.直接的方法用code

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;              //可以使用ctrl+shift+o这个快捷键来自动添加

public class HelloWord extends Activity{

private TextView myTextView;

public void onCreate(Bundle savedInstanceState){

super.onCreate(saveInstanceState);

myTextView = new TextView(this);

myTextView.setText("Hello World");

setContentView(myTextView);

//原来的是setContentView(R.layout.main);这个是在res/Layout/main.xml这个布局文件

}

}

2,通过XML文件

当我们新建一个工程的时候,java文件中setContentView中的内容为R.layout.main,当我们按住ctrl然后用鼠标单击的时候会跳转到main.xml这个文件中,这个就是我们在这个Activity中的布局文件,在这里做布局,将对我们的视图影响,我们现在对这个布局进行重新设计

首先是xml文件的编写

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

<LinearLayout

xmlns:android = http://schemas.android.com/apk/res/android
android:orientation="vertical"

android:layout_width="fill_parent"

androiud:layout_height="fill_parent">

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

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

android:typeface  字体

android:textStyle  bold italic

android:textColor 字体颜色

android:textSize    字体大小

android:singleLine 是否单行

android:gravity   内容的定位

//对于android:textColor我们可以定义一个button_color的xml文件,让其在选中,按下,正常的状态是有不同的颜色值

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:color="#00ff00" android:state_focused="true"/>

<item android:color="#ff0000" android:state_pressed="true"/>

<item android:color="#0000ff" android:state_pressed="false"/>

</selector>

然后就在android:textColor="@layout/button_color";

</LinearLayout>

然后再res/values/string.xml下进行一些修改就搞定啦!

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

<resources>

<string name="hello">Hello World!</string>

<string name="hell0_world">"您好"</string>

</resources>

这也是一个实现方式!

3.通过Id找对象,然后处理

首先要再main.xml文件中,对TextView中增加一点

android:id="@+id/myTextView"

然后再java文件中改为

mytextView = (TextView)findViewByid(R.id.myTextView);

mytextView.setText("您好");

ok,搞定啦!

如果有新的内容将不断更新、。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TextView android