Android 为自定义线性布局将布局重心设置为中心

Android 为自定义线性布局将布局重心设置为中心,android,android-linearlayout,custom-component,Android,Android Linearlayout,Custom Component,我正在创建一个自定义线性布局,只是为了测量并正确调整其大小: 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; } @Ove

我正在创建一个自定义线性布局,只是为了测量并正确调整其大小:

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);
}
}
它工作得很完美,但它显然在测量值改变之前保持其位置,因此不再水平居中。如何替代自定义线性布局中的设置,使其显示在水平中心

这就是我在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" >

但很明显,在措施改变之前,它一直保持着自己的立场——你能更清楚地知道发生了什么吗?是否要将此自定义
线性布局水平居中?是,我希望它水平居中。布局,而不是它的子项。您是否尝试了自定义视图的android:layout\u gravity=“center”
?啊,这很有帮助。我真傻。我把它放在那里,然后就不见了:)谢谢。