我正在创建一个自定义 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" >