android基礎-TextView

對於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,搞定啦!


如果有新的內容將不斷更新、。。。。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章