想學習android很久了,也看了很多的資料和視頻,但是總結一點學習任何語言都不是去被動的吸收知識,而要主動的學習。何爲主動的學習,做IT這一行的就是要自己主動的敲代碼,敲代碼敲着敲着你就有了感情,你就會不知不覺的理解了。當你用代碼實現的有結果的具體的理解要比別人在哪給你講的抽象的被動的吸收要來的直接要好。所以我準備通過自己不斷的敲,去不斷的學習。這系列文件旨在記錄自己的學習工程,也爲了鞏固自己學習的知識。好了,從現在開始吧,Just do it。。。
TextView 是android中的最基礎也是最終要的組件。
先來看看效果,實現的效果是新建一個文本框,並是文本框中的文字實現向下滾動的效果
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">TextViewProject</string>
- <string name="hello_world">
- Hello world!
- 想實現textview上下滑動的功能,也確實做到了,但是效果不盡如人意,具體是這樣的: 發現textview添加上setMovementMethod這個方法後,上下滑動的時候背景就會變灰,
- 手擡起後就又回覆正常,當然也並不是所有機型都有這種情況,我測了7部手機,發現只有moto(Me525 android2.2)跟三星(GT-N700 android2.3)的機子上不會出現這種情況,
- 其他機型(中興(4.0.3),htc G8(2.3),htc G7(4.0),三星galaxy nexus(4.0.4) )誰知道這是什麼原因呢 因爲這裏不能插入圖片,所以想看效果的去這裏
- </string>
- <string name="menu_settings">Settings</string>
- </resources>
- <RelativeLayout 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"
- tools:context=".TextViewActivity" >
- <TextView //添加一個文本框
- android:id="@+id/myTextView"
- android:layout_width="fill_parent" //設置文本框的寬度是手機屏幕的寬度
- android:layout_height="fill_parent" //高度的手機的屏幕的高度
- android:textSize="30dp" //設置文本的內容字體大小
- android:scrollbars="vertical" //設置滑條是水平還是垂直的,這裏是設置爲垂直的
- android:text="@string/hello_world" /> //設置文本的內容,就是我們剛剛加如的文本的內容
- </RelativeLayout>
TextViewActivity.java文件
- package com.cheng.textviewproject;
- import android.os.Bundle;
- import android.app.Activity;
- import android.text.method.ScrollingMovementMethod;
- import android.view.Menu;
- import android.widget.TextView;
- public class TextViewActivity extends Activity {
- private TextView mTextView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.main);
- //通過id號找到我們設置的TextView組件
- mTextView = (TextView)findViewById(R.id.myTextView);
- //設置文本框的字體爲可滾動的方式,主要是這裏
- mTextView.setMovementMethod(ScrollingMovementMethod.getInstance());
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Ok,今天到此,明天繼續。