android組件之TextView

        想學習android很久了,也看了很多的資料和視頻,但是總結一點學習任何語言都不是去被動的吸收知識,而要主動的學習。何爲主動的學習,做IT這一行的就是要自己主動的敲代碼,敲代碼敲着敲着你就有了感情,你就會不知不覺的理解了。當你用代碼實現的有結果的具體的理解要比別人在哪給你講的抽象的被動的吸收要來的直接要好。所以我準備通過自己不斷的敲,去不斷的學習。這系列文件旨在記錄自己的學習工程,也爲了鞏固自己學習的知識。好了,從現在開始吧,Just do it。。。

TextView 是android中的最基礎也是最終要的組件。

先來看看效果,實現的效果是新建一個文本框,並是文本框中的文字實現向下滾動的效果

 string.xml文件:
 在string.xml文件中加入你自己想顯示的string,
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="app_name">TextViewProject</string> 
  4.     <string name="hello_world"> 
  5.         Hello world!  
  6.         想實現textview上下滑動的功能,也確實做到了,但是效果不盡如人意,具體是這樣的: 發現textview添加上setMovementMethod這個方法後,上下滑動的時候背景就會變灰,  
  7. 手擡起後就又回覆正常,當然也並不是所有機型都有這種情況,我測了7部手機,發現只有moto(Me525 android2.2)跟三星(GT-N700 android2.3)的機子上不會出現這種情況,  
  8.  其他機型(中興(4.0.3),htc G8(2.3),htc G7(4.0),三星galaxy nexus(4.0.4) )誰知道這是什麼原因呢 因爲這裏不能插入圖片,所以想看效果的去這裏               
  9.         </string> 
  10.     <string name="menu_settings">Settings</string> 
  11. </resources> 
 
layout中的main.xml文件
在佈局文件中加入一個文本框
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     tools:context=".TextViewActivity" > 
  6.  
  7.     <TextView    //添加一個文本框  
  8.         android:id="@+id/myTextView" 
  9.         android:layout_width="fill_parent"  //設置文本框的寬度是手機屏幕的寬度  
  10.         android:layout_height="fill_parent" //高度的手機的屏幕的高度  
  11.         android:textSize="30dp"   //設置文本的內容字體大小  
  12.           
  13.         android:scrollbars="vertical"   //設置滑條是水平還是垂直的,這裏是設置爲垂直的  
  14.         android:text="@string/hello_world" />     //設置文本的內容,就是我們剛剛加如的文本的內容  
  15.  
  16. </RelativeLayout> 

 

TextViewActivity.java文件

 

  1. package com.cheng.textviewproject;  
  2.  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.text.method.ScrollingMovementMethod;  
  6. import android.view.Menu;  
  7. import android.widget.TextView;  
  8.  
  9. public class TextViewActivity extends Activity {  
  10.     private TextView mTextView;  
  11.     @Override 
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         super.setContentView(R.layout.main);  
  15.         //通過id號找到我們設置的TextView組件  
  16.         mTextView = (TextView)findViewById(R.id.myTextView);  
  17.         //設置文本框的字體爲可滾動的方式,主要是這裏  
  18.         mTextView.setMovementMethod(ScrollingMovementMethod.getInstance());  
  19.     }  
  20.  
  21.     @Override 
  22.     public boolean onCreateOptionsMenu(Menu menu) {  
  23.         // Inflate the menu; this adds items to the action bar if it is present.  
  24.           
  25.         getMenuInflater().inflate(R.menu.main, menu);  
  26.         return true;  
  27.     }  
  28.  
  29. }  

Ok,今天到此,明天繼續。

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