当我从微调器中选择值时,我有一个旋转器,其字体大小为14dp,16dp等 我想在运行时更改应用程序的所有活动的文本视图中的字体大小。 这有可能吗?
我提到了几个答案,但它对我不起作用..
答案 0 :(得分:2)
试试这段代码:
package com.packegename;
import android.app.Activity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class AndroidTextSize extends Activity {
String[] optUnit = {"COMPLEX_UNIT_DIP",
"COMPLEX_UNIT_IN",
"COMPLEX_UNIT_MM",
"COMPLEX_UNIT_PT",
"COMPLEX_UNIT_PX",
"COMPLEX_UNIT_SP"};
int[] valueUnit = { TypedValue.COMPLEX_UNIT_DIP,
TypedValue.COMPLEX_UNIT_IN,
TypedValue.COMPLEX_UNIT_MM,
TypedValue.COMPLEX_UNIT_PT,
TypedValue.COMPLEX_UNIT_PX,
TypedValue.COMPLEX_UNIT_SP};
String[] optSize ={"0.05", "0.1", "0.25", "0.5", "1", "4", "8", "10", "14", "16", "20", "30"};
float[] valueSize = {0.05f, 0.1f, 0.25f, 0.5f, 1f, 4f, 8f, 10f, 14f, 16f, 20f, 30f};
Spinner selUnit, selSize;
TextView textOut;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selUnit = (Spinner)findViewById(R.id.selunit);
selSize = (Spinner)findViewById(R.id.selsize);
textOut = (TextView)findViewById(R.id.textout);
ArrayAdapter<String> adapterUnit = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, optUnit);
adapterUnit.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selUnit.setAdapter(adapterUnit);
selUnit.setOnItemSelectedListener(selUnitOnItemSelectedListener);
ArrayAdapter<String> adapterSize = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, optSize);
adapterSize.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selSize.setAdapter(adapterSize);
selSize.setOnItemSelectedListener(selSizeOnItemSelectedListener);
}
private Spinner.OnItemSelectedListener selUnitOnItemSelectedListener
= new Spinner.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
updateTextSize();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}};
private Spinner.OnItemSelectedListener selSizeOnItemSelectedListener
= new Spinner.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
updateTextSize();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}};
private void updateTextSize(){
int unit = valueUnit[selUnit.getSelectedItemPosition()];
String strUnit = optUnit[selUnit.getSelectedItemPosition()];
float size = valueSize[selSize.getSelectedItemPosition()];
String strSize = optSize[selSize.getSelectedItemPosition()];
textOut.setTextSize(unit, size);
textOut.setText(strUnit + " : " + strSize);
}
}
上面的代码适用于一个活动以及单个textview或其他视图。 全局使用此代码进行整个应用程序活动修改
创建一个类。 TextFontSize.class 实现可序列化的类
将 updateTextSize()
更改为。
public static void updateTextView(TextView theTextView, float theSize) {
theTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, theSize);
}
public static void updateEditText(EditText theEditText, float theSize) {
theEditText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, theSize);
}
和任何其他布局视图只需创建上述方法。
并使用此功能应用程序的任何活动,如
private void setAllFontSizeOf(float theSize) {
TextVFontsize.updateTextView(textview, theSize);
TextFontSize.updateEditText(Edittext,theSize);
}
最后将此方法声明为特定活动。