2014-02-12 56 views

回答

0

我發現,這是否使用捏放大答案:https://stackoverflow.com/a/14306988/1683141

我不知道這是否是necesairy爲你實際上有/縮小按鈕放大或者捏縮放會做:

public class MainActivity extends Activity { 

    TextView scaleGesture; 
    ScaleGestureDetector scaleGestureDetector; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     scaleGesture = (TextView)findViewById(R.id.article); 
     scaleGesture.setText("this is some text"); 
     scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener()); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 


    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // TODO Auto-generated method stub 
     scaleGestureDetector.onTouchEvent(event); 
     return true; 
    } 

    public class simpleOnScaleGestureListener extends 
      SimpleOnScaleGestureListener { 

     @Override 
     public boolean onScale(ScaleGestureDetector detector) { 
      // TODO Auto-generated method stub 
      float size = scaleGesture.getTextSize(); 
      Log.d("TextSizeStart", String.valueOf(size)); 

      float factor = detector.getScaleFactor(); 
      Log.d("Factor", String.valueOf(factor)); 


      float product = size*factor; 
      Log.d("TextSize", String.valueOf(product)); 
      scaleGesture.setTextSize(TypedValue.COMPLEX_UNIT_PX, product); 

      size = scaleGesture.getTextSize(); 
      Log.d("TextSizeEnd", String.valueOf(size)); 
      return true; 
     } 
    } 
} 

而且,這裏是動態設置TextView的佈局屬性(所以你也可以使用它的字體大小):https://stackoverflow.com/a/4632208/1683141

myTextView.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view){ 
      //highlight the TextView 
      myTextView.setTextAppearance(getApplicationContext(), R.style.boldText); 
      myTextView.setBackgroundResource(R.color.highlightedTextViewColor); 
     } 
    }); 

Ofcou RSE,你需要在style.xml

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

    <style name="boldText"> 
     <item name="android:textStyle">bold|italic</item> 
     <item name="android:textColor">#FFFFFF</item> 
    </style> 

    <style name="normalText"> 
     <item name="android:textStyle">normal</item> 
     <item name="android:textColor">#C0C0C0</item> 
    </style> 

</resources> 

如下因素風格引用,並添加如下因素,以這樣的「的strings.xml」文件:

<color name="highlightedTextViewColor">#000088</color> 
<color name="normalTextViewColor">#000044</color> 
相關問題