运行效果预览图:
初始界面变为小字号
变为大字号
xml布局文件源代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Button
android:id="@+id/small"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="大字号"/>
<Button
android:id="@+id/big"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="小字号"/>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="你好呀,小仙女!"/>
</LinearLayout>
java文件源码:
package com.example.wordsize;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button big = (Button)findViewById(R.id.big);
Button small = (Button)findViewById(R.id.small);
tv = (TextView)findViewById(R.id.tv);
big.setOnClickListener(this); //给按钮设置监听
small.setOnClickListener(this); //给按钮设置监听
}
public void onClick(View v){
switch (v.getId()){
case R.id.big: //当点击了大字号按钮时
tv.setTextSize(10); //将字号设置为10
break;
case R.id.small: //当点击了小字号按钮时
tv.setTextSize(30); //将字号设置为30
break;
default:
break;
}
}
}
网友评论