2009-09-02 7 views
41

Jak korzystać viewGroup w androidJak korzystać viewGroup w android

+2

Jak chcesz go używać? –

+0

[tutaj] (http://developer.android.com/reference/android/view/ViewGroup.html) to dokumentacja od Google – Vinay

+0

http://about-android.blogspot.com/2010/05/create-dynamic -view-group.html Ten przykład jest OK. Pamiętaj, aby ustawić unikalne id-s dla takich widoków podczas korzystania z nich w 'ListView', lub mogą być przerysowane podczas przewijania –

Odpowiedz

31

Jak wspomniano, ViewGroup jest klasą abstrakcyjną, że wszystkie ViewGroups przedłużyć, LinearLayout na przykład jest ViewGroup.

MyViewGroup.java:

public class MyViewGroup extends LinearLayout { 

    public MyViewGroup(Context context) { 
     super(context); 
    } 

    public MyViewGroup(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MyViewGroup(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     super.onLayout(changed, l, t, r, b); 
     Log.e("SWIPED", "onLayout : " + Boolean.toString(changed)); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent event) { 
     super.onInterceptTouchEvent(event); 
     Log.e("SWIPED", "onInterceptTouchEvent : " + event.getAction()); 
     return false; 
    } 
} 

Main.XML:

<com.example.MyViewGroup xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/screen" android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 

<TextView android:id="@+id/tv1" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:text="TEXT ONE" 
    android:padding="20dip" android:background="@android:color/background_dark" /> 

<TextView android:padding="20dip" android:background="@android:color/background_light" 
    android:id="@+id/tv2" android:layout_height="wrap_content" 
    android:text="TEXT TWO" android:layout_width="fill_parent" /> 

</com.example.MyViewGroup> 

MainActivity.java:

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 
+0

Klasa MyViewGroup tutaj nic nie robi, nic nie drukuje. – user606669

+3

Nie sądzę, że ta odpowiedź może pomóc nikomu. Ponieważ rozszerza "LinearLayout", a wszystkie te zadania powinny zostać nadpisane, jest rzucane do 'LinearLayout'. Nie ma zmysłów. – hguser

+0

Chodzi o to, aby pokazać, czym jest ViewGroup, za pomocą kodu zastępczego. Nic dodać nic ująć. – worked

Powiązane problemy