2011年7月19日星期二

動態改變文字的大小, setTextSize()

實例:

動態改變文字的大小, setTextSize()

package com.AndroidText;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidTextActivity extends Activity {

TextView text;
Button buttonChangeTextSize;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.text);
buttonChangeTextSize = (Button)findViewById(R.id.changetextsize);

buttonChangeTextSize.setOnClickListener(new Button.OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
//Change Text Size of TextView
text.setTextSize(50);
//Change Text Size of Button
buttonChangeTextSize.setTextSize(35);
}});
}
}


<?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"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/changetextsize"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Text Size"
/>
</LinearLayout>


沒有留言:

發佈留言