我正在创建一个自定义LinearLayout来测量并正确调整它的大小:
public class Frame extends LinearLayout
{
private int width;
private int height;
private Context context;
public Frame(Context context, AttributeSet attrs)
{
super(context, attrs);
this.context=context;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int height = MeasureSpec.getSize(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int newwidth=MeasureSpec
.makeMeasureSpec(height*480/720,MeasureSpec.EXACTLY);
super.onMeasure(newwidth, heightMeasureSpec);
}
}
它完美地工作,但它显然在测量改变之前保持其位置,因此它不再水平居中。如何覆盖自定义LinearLayout中的设置以使其显示在水平中心?
这是我在xml文件中调用它的方式:
com.myproject.main.Frame
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/button"
android:layout_marginBottom="10dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="480" >