2014-03-07 77 views
0

我有一個線性佈局,其中包含TextView的數量。如果我們觸及它,每個文本視圖都會更改其文本大小。如何在拖動LinearLayout時更改TextView的TextView大小

我想這樣做,如果我觸摸第一個,並保持拖動線性佈局,而不拉我的手指。有我的手指上的TextView應該改變它的大小,如果我從那個textView中拖拽它的大小應該是正常的。

我有MainActivity:

public class MainActivity extends Activity implements OnTouchListener{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView firstText, secondText; 
     firstText = (TextView) findViewById(R.id.firstTextView); 
     secondText = (TextView) findViewById(R.id.secondTextView); 

     firstText.setOnTouchListener(this); 
     secondText.setOnTouchListener(this); 

    } 


    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     switch(event.getAction() & MotionEvent.ACTION_MASK) { 

      case MotionEvent.ACTION_DOWN: 
      case MotionEvent.ACTION_POINTER_DOWN: 
       ((TextView) v).setTextSize(40); 
       break; 

      case MotionEvent.ACTION_CANCEL: 
      case MotionEvent.ACTION_UP: 
      case MotionEvent.ACTION_POINTER_UP: 

       ((TextView) v).setTextSize(20); 
       break; 
     } 
     return false; 
    } 

} 

XML是:

<LinearLayout 
     android:id="@+id/ll" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/btn_bg" 
     android:orientation="horizontal" 
     android:weightSum="0.9" > 

     <TextView 
      android:id="@+id/firstTextView" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="@string/hello_world" /> 

     <TextView 
      android:id="@+id/secondTextView" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="@string/hello_world"/> 
    </LinearLayout> 

請幫助。我想要的行爲,如果我觸摸firstTextView的大小,我長大了,並將手指移動到secondTextView手指超出firstTextView的界限時,其文本大小變得正常,沒有拉下我的手指我繼續前進到secondTextView,只要我的手指進入secondTextView的邊界其大小被改變。

問題是當手指進入secondTextView它不會生效。

+0

任何教程都會非常有幫助。在此先感謝 – user2539635

+0

爲TextView使用setonTouchListener方法。 – Piyush

+0

我認爲這個教程是有幫助的http://examples.javacodegeeks.com/android/core/ui/drag-and-drop/android-drag-and-drop-example/ –

回答

0

你可以在你的textView上調用setOnTouchListener。然後根據您希望尺寸增長的方式,使用傳入的觸摸事件中的字段來記錄觸摸的時間或移動的距離,以更改文本視圖的大小。

相關問題