如何在android中使用viewGroup

时间:2009-09-02 14:17:39

标签: android

如何在android中使用viewGroup

1 个答案:

答案 0 :(得分:30)

如上所述,ViewGroup是一个所有ViewGroups都扩展的抽象类,例如LinearLayout是一个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);
    }
}