顯示具有 android -- TextView 標籤的文章。 顯示所有文章
顯示具有 android -- TextView 標籤的文章。 顯示所有文章

2012年11月18日 星期日

捲動 TextView 可以捲動 (scroll) 內容


main.java:

contentTextView = (TextView) findViewById(R.id.contentTextView);
contentTextView.setMovementMethod(ScrollingMovementMethod.getInstance()); //可以Scroll textview 的內容


res/layout/main_activity_layout.xml:


<TextView
        android:id="@+id/textView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:maxLines="15"
        android:scrollbarStyle="outsideInset"
        android:scrollbars="vertical"
        android:singleLine="false"
        android:text=""
        android:textColor="#00FF00"
        android:textSize="20dp" />

2012年5月19日 星期六

TextView 滾動 ─利用 ScrollView


只要用 ScrollView 包起來,TextView 裡的內容便可以滾動。



res/layout/main.xml:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="測試 TextView 的內容可以滾動"
        android:textSize="25dp" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fadingEdge="vertical"
        android:paddingTop="10dip"
        android:scrollbars="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dip"
            android:text="@string/text_content"
            android:textSize="20dp" />
    </ScrollView>

</LinearLayout>

*******************************

res/values/strings:

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

    <string name="hello">Hello World, Cq_TextView01Activity!</string>
    <string name="app_name">Cq_TextView01</string>
    <string name="text_content">歐美經濟情勢動盪不安,連帶影響國際油價持續走跌,預計台灣中油於下周一起,汽柴油價格可望降價1角,是從上個月油價調漲後,連續第7次調價。
現在的92無鉛汽油每公升是32.6元,自4月2日一次調漲2.3元;而95無鉛每公升為34.1元,從4月2日一次調升3.1元;此外,98無鉛每公升為36.1元,4月2日則一次增3.6元;柴油每公升是31.7元,4月2日一次調漲3.2元。
國際油價走跌,指標原油價格自上周每桶109.92美元跌到108.67美元;依據浮動油價公式以及減半調降機制初步估計,台灣中油從下周一凌晨起,可望再度調降國內汽柴油的價格,每公升調降約1角。
    </string>

</resources>



2012年5月17日 星期四

Text String 變色


Text String

public class TextViewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        TextView tv = new TextView (this);
        String string = "Ilovemydog"; //10 letters
        SpannableStringBuilder fontStyleBuilder = new SpannableStringBuilder(string);
        fontStyleBuilder.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //1-0 = 1 letter-> I
        fontStyleBuilder.setSpan(new ForegroundColorSpan(Color.RED), 1, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//5-1 = 4 letters-> love
        fontStyleBuilder.setSpan(new ForegroundColorSpan(Color.GREEN), 5, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//7-5 = 2 letters -> my
        fontStyleBuilder.setSpan(new ForegroundColorSpan(Color.YELLOW), 7, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//10-7 = 3 letters -> dog
        tv.setText(fontStyleBuilder);
        tv.setTextSize(24);
        setContentView(tv);
    }
}