package com.example.two;
import com.example.two.utils.UiUtils;
import android.app.Activity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView main_tv1;
private TextView main_tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_tv1 = (TextView) findViewById(R.id.main_tv1);
main_tv2 = (TextView) findViewById(R.id.main_tv2);
}
public void setFontSize(View v){
main_tv1.setText("中国");
main_tv1.setTextSize(UiUtils.dip2px(this, 28));
//
// main_tv2.setText("中国");
// main_tv2.setTextSize(84, TypedValue.COMPLEX_UNIT_SP);
}
public void setFontSize2(View v){
main_tv2.setText("中国");
main_tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP,28*3);
// main_tv2.setTextSize(unit, size);
}
}
布局:
<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"
>
<Button
android:text="设置"
android:layout_centerInParent="true"
android:onClick="setFontSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_btn1"
/>
<TextView
android:id="@+id/main_tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn2"
android:layout_alignBaseline="@id/main_btn1"
android:layout_toRightOf="@id/main_btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setFontSize2"
android:text="设置2" />
<TextView
android:layout_toRightOf="@id/main_tv1"
android:id="@+id/main_tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>