【发布时间】:2013-07-16 07:04:12
【问题描述】:
我在以编程方式从我指定的 XML 布局中扩展我的 CustomView 时遇到问题。
我有一个CustomView,它扩展RelativeLayout 并包含另一个RelativeLayout,而RelativeLayout 又包含2 个ImageView 和1 个LinearLayout。 ImageViews 是箭头图标,我分别通过 android:layout_alignParentLeft="true" 和 android:layout_alignParentRight="true" 放在父级的左侧和右侧,LinearLayout 用于填充其间的所有空间。
为了清楚起见,这里是 Eclipse 布局设计器中的 xml 布局视图,这正是我想要的......
如果我 setContentView(R.layout.my_xml_layout);直接从 Activity 中,一切都显示在 Eclipse Layout Designer 中,但是,如果我从 CustomView 的构造函数中对 R.layout.my_xml_layout 进行膨胀,则 ImageView 的左右有一个无法消失的顽固边距。
这是在java代码中完成的,有问题:
任何帮助将不胜感激!提前致谢!
my_xml_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<ImageView
android:id="@+id/imageLeftArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/dock_leftarrow" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ImageView
android:id="@+id/imageRightArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/dock_rightarrow" />
</RelativeLayout>
</RelativeLayout>
我通过这一行在CustomView的构造函数中给它充气:
View.inflate( mContext, R.layout.my_xml_layout, this );
我的 CustomView 的 onLayout:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// Do nothing. Do not call the superclass method--that would start a layout pass
// on this view's children. PieChart lays out its children in onSizeChanged().
Log.e("DrawView", "DrawView.onLayout: " + l + ", " + t + ", " + r + ", " + b);
int iChildCount = this.getChildCount();
for ( int i = 0; i < iChildCount; i++ ) {
View pChild = this.getChildAt(i);
pChild.layout(0, 0, pChild.getMeasuredWidth(), pChild.getMeasuredHeight());
}
}
我的 CustomView 的 onMeasure:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Try for a width based on our minimum
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.d("DockView", "DockView.onMeasure: width: " + widthMeasureSpec + " getWidth: " + MeasureSpec.getSize(widthMeasureSpec));
Log.d("DockView", "DockView.onMeasure: height: " + heightMeasureSpec + " getHeight: " + MeasureSpec.getSize(heightMeasureSpec));
Log.d("DockView", "DockView.onMeasure: getPaddingLeft: " + getPaddingLeft() + " getPaddingRight: " + getPaddingRight());
Log.d("DockView", "DockView.onMeasure: getPaddingTop: " + getPaddingTop() + " getPaddingBottom: " + getPaddingBottom());
// http://stackoverflow.com/a/17545273/474330
int iParentWidth = MeasureSpec.getSize(widthMeasureSpec);
int iParentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(iParentWidth, iParentHeight);
int iChildCount = this.getChildCount();
for ( int i = 0; i < iChildCount; i++ ) {
View pChild = this.getChildAt(i);
this.measureChild( pChild,
MeasureSpec.makeMeasureSpec(iParentWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(iParentHeight, MeasureSpec.EXACTLY)
);
}
}
【问题讨论】:
-
你有任何填充或其他东西吗?
-
不,没有填充,根本没有边距......我怀疑它是 onMeasure,因为如果我将其注释掉,ImageView 将根本不会显示......我从另一个得到代码StackOverflow 中的问题,并没有真正理解它(MeasureSpec 等)的工作方式......
-
从你的第二个
RelativeLayout中删除这行xmlns:android="http://schemas.android.com/apk/res/android"。 -
我删除了它,它没有任何区别:(
标签: android android-layout layout