80,471
社区成员




<com.example.swipeview.MyViewGroup
android:id="@+id/swap_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:src="@drawable/ic_action_search"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
</com.example.swipeview.MyViewGroup>
public class MyViewGroup extends ViewGroup {
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyViewGroup(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int count = this.getChildCount();
for(int i = 0;i < count;i++){
View child = this.getChildAt(i);
child.measure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onLayout(boolean arg0, int l, int t, int r, int b) {
int count = this.getChildCount();
for(int i = 0;i < count;i++){
View child = this.getChildAt(i);
child.setVisibility(View.VISIBLE);
//child.measure(r-l, b-t);
int x = l;
int y = t;
//心中疑惑:我认为我只要在这里调用child的layout(int,int,int,int)方法给出它的位置和宽高就可以了
//如果child本身是一个ViewGroup的话它是否应该自己去管理它本身内部的child的位置和宽度呢???
child.layout(x,y,x + getWidth(),y + getHeight());
}
}
}
public class MyViewGroup extends FrameLayout {
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyViewGroup(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean arg0, int l, int t, int r, int b) {
int count = this.getChildCount();
for(int i = 0;i < count;i++){
View child = this.getChildAt(i);
child.setVisibility(View.VISIBLE);
//child.measure(r-l, b-t);
int x = l;
int y = t;
//心中疑惑:我认为我只要在这里调用child的layout(int,int,int,int)方法给出它的位置和宽高就可以了
//如果child本身是一个ViewGroup的话它是否应该自己去管理它本身内部的child的位置和宽度呢???
child.layout(x,y,x + getWidth(),y + getHeight());
}
}
}