2016-04-02 21 views
0

Bir Circle çizmek için DrawCircle View Sınıfı oluşturdum. Bu DrawCircle View Sınıfını bir fragmana eklemeyi seviyorum. DrawCircle View sınıfı aşağıdaki gibidir.Add view in Android

public class DrawCircle extends View { 

    private Paint paint; 

    public DrawCircle(Context context) { 
     super(context); 
     // create the Paint and set its color 
     paint = new Paint(); 
     paint.setColor(Color.WHITE); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawColor(Color.BLUE); 
     canvas.drawCircle(200, 200, 100, paint); 
    } 
} 

Benim parçamın içinde zaten şişirilmiş bir Düzenim var.

public class GripForce extends Fragment{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 


    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     v = inflater.inflate(R.layout.fragment_grip_force, container, false); 
     final Button buttonAcce = (Button) v.findViewById(R.id.gripbutton); 
     buttonAcce.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       if (vibrate == false) { 
        vibrate = true; 
        buttonAcce.setText("VIBRATE STOP"); 
        // Vibrate for 300 milliseconds 
        timer = new Timer(); 
        myTimerTask = new MyTimerTask(); 
        timer.schedule(myTimerTask, 1, 80000); 
        mVibrator.vibrate(100000); 
       } else { 
        timer.cancel(); 
        timer.purge(); 
        mVibrator.cancel(); 
        vibrate = false; 
        buttonAcce.setText("VIBRATE"); 

       } 

      } 
     }); 
     mVibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE); 


     return v; 
    } 

} 

Sorunuz, bu DrawCirle Görünümü'nün mevcut Görünüm'ün üzerine nasıl alınacağıdır.

Şimdiden teşekkürler.

DÜZENLEME: Görünümü xml'de ayarlıyorum.

<FrameLayout 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" 
    tools:context="com.forcetest.GripForce"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal|top"> 

     <com.abbott.forcetest.DrawCircle 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/circleView" 
      android:layout_weight="1"/> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="65dp" 
     android:layout_gravity="center_horizontal|bottom"> 



     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="VIBRATE" 
      android:id="@+id/gripbutton" 
      android:background="#e71919" 
      android:layout_marginLeft="30dp" 
      android:layout_marginRight="30dp" 
      android:layout_marginBottom="20dp" /> 
    </LinearLayout> 


</FrameLayout> 

Sonra onCreateView içinde, programın hattı

v = inflater.inflate(R.layout.fragment_grip_force, container, false); 

worng nedir iflas etti?

EDIT2

: Ben Artık çalışır

<FrameLayout 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" 
    tools:context="com.abbott.forcetest.GripForce" 
    android:id="@+id/gripLayout"> 

    <!--<LinearLayout--> 
     <!--android:orientation="vertical"--> 
     <!--android:layout_width="match_parent"--> 
     <!--android:layout_height="match_parent"--> 
     <!--android:layout_gravity="center_horizontal|top">--> 

     <!--<com.abbott.forcetest.DrawCircle--> 
      <!--android:layout_width="wrap_content"--> 
      <!--android:layout_height="wrap_content"--> 
      <!--android:id="@+id/circleView"--> 
      <!--android:layout_weight="1"/>--> 
    <!--</LinearLayout>--> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="65dp" 
     android:layout_gravity="center_horizontal|bottom"> 
     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="VIBRATE" 
      android:id="@+id/gripbutton" 
      android:background="#e71919" 
      android:layout_marginLeft="30dp" 
      android:layout_marginRight="30dp" 
      android:layout_marginBottom="20dp" /> 
    </LinearLayout> 


</FrameLayout> 



Then in the program, 

final FrameLayout l_out = (FrameLayout) v.findViewById(R.id.gripLayout); 
DrawCircle circleVIew = new DrawCircle(this.getContext()); 
l_out.addView(circleVIew); 

benim xml değiştirdi.

cevap

1

Bunu dinamik olarak başka görünümlere eklemek istediğiniz gibi ekleyin.

Sadece oluşturmak -
DrawCircle circle = new DrawCircle(context); 

Sonra senin Ebeveyninizin düzeni eklemek - buna göre yerleştirmek için daire görünümü eklerken istediğiniz

yourParentLayout.addView(circle); 

Set herhangi özelliklerini.

Görünümler dinamik olarak nasıl eklenir here.

0

Görünümü kendi kendine tanımlamaya çalışıyorsunuz. Ve şimdi her şeyden önce çizim ile ilgili her şeyi hazırladığınızı varsayarak (çiziminizi görüntülemek için kodunuzu kontrol etmedim) ve şimdi bu görünümü yerleştirmek için yapmanız gereken şey sadece düzen dosyanızda kodlamaktır. Bunun gibi

: Eğer TextView'un veya Düğme, düzen xml dosyasında kod bunu ve java kodunda findViewById hatırlamak ne gibi

<com.example.yourprojectname.DrawCircle 
    android:layout_width=xxxx 
    android:layout_height=xxx 
    /> 

. Görünecektir.

Ve tabiki programlamada yapabilirsiniz, kodunuza addView yöntemini deneyin. Ancak xml dosyasına yazmanız biraz daha kolay, ayrıca o görünüme geçmek istediğiniz konumu tanımlayabilirsiniz.

+0

EDIT'de önerdiğiniz ve güncellediğiniz gibi denedim. Ama program çöktü. Ne yanlış olabilir? – batuman

İlgili konular