80,471
社区成员




package com.yht.testViewClass;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
public class testViewClass extends Activity {
/** Called when the activity is first created. */
MyViewGroup mvg;
LayoutInflater layoutInflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mvg = new MyViewGroup(this);
this.setContentView(mvg);
layoutInflater = this.getLayoutInflater();
RelativeLayout view = (RelativeLayout) layoutInflater.inflate(R.layout.mute_box, null);
mvg.addView(view);
}
}
package com.yht.testViewClass;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;
public class MyViewGroup extends ViewGroup {
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyViewGroup(Context context) {
super(context);
}
@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());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@drawable/list_background">
<!--
Button 相对于Parent 垂直靠右显示
-->
<Button
android:id="@+id/image_button_2"
android:text="TEST BUTTON"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>