2013-01-22 14 views
8

Birinci için tost Dinamik konumunu ayarlayın i onay kutusunu yanında tost görüntülemek istediğiniz sorun şu ki Yani Şimdi tam koda View Tüm bunların

@Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
     Toast toast=Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT); 
     toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0); 
     View view=toast.getView(); 
     switch(buttonView.getId()) 
     { 
      case R.id.chkRed: if(chkRed.isChecked()) 
      { 
           chkGreen.setChecked(false); 
           chkBlue.setChecked(false); 
           chkYellow.setChecked(false); 
           toast.setText("You Selected Red"); 
           toast.setGravity(Gravity.NO_GRAVITY,buttonView.getRight(), chkRed.getTop()); 
          //top does not align 
          //it align perfectly to right of checkbox 
           view.setBackgroundColor(Color.RED); 
      } 
          break; 
} 
} 

değil, ben setGravity() ile çalışan, ama sadece o çalıştı Çalışmıyor ve uzun süredir çalışıyor ve çalışıyor, ancak ilerleme yok

Onay kutusunun yanında nasıl tost görüntülenir.

cevap

10

Tamam, nihayet,

daha Referans için
buttonView.getLocationOnScreen(location); 

tarafından, onay kutusu veya başka bir görünümün

Sen bakış ekranında yerini almak gerekir

1. yanında tost nasıl anladım

kullanarak tost getLocationOnScreen(int[])

2. Seti yerçekimi bakın 0

toast.setGravity(Gravity.TOP|Gravity.LEFT,buttonView.getRight()+5, 
location[1]-10); 

Burada önemli olan bakış sağ örneğin buttonView.getRight() için tost x-coordinate ayarlanır ve getLocationOnScreen()

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
     Toast toast=Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT); 
     toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0); 
     View view=toast.getView(); 
     int location[]=new int[2]; 
     buttonView.getLocationOnScreen(location); 
     switch(buttonView.getId()) 
     { 
      case R.id.chkRed: if(chkRed.isChecked()) 
      { 
           chkGreen.setChecked(false); 
           chkBlue.setChecked(false); 
           chkYellow.setChecked(false); 
           toast.setText("You Selected Red"); 
           toast.setGravity(Gravity.TOP|Gravity.LEFT,buttonView.getRight()+5, location[1]-10); 
           view.setPadding(1, 1, 1, 1); 
           view.setBackgroundColor(Color.RED); 
      } 

     } 
    } 
+4

Bu cevap http aldığım location[1] gelen y koordinatları olsun: // stackoverflow .com/a/21026866/630833, Toast'ın boyutunu göz önünde bulundurarak, bana yardımcı oldu. – jayeffkay