!!!问题解决了!!!
从代码中删除它后,问题被覆盖了 onLayout 函数,一切都按预期工作!
我创建了自己的 View 对象扩展 RelativeLayout,它显示但它应用了一些额外的填充(如下图) - 灰色区域是添加到主 XML 布局的自定义元素的大小(大小符合预期),绿色是视图膨胀到根据上面元素的高度以某种方式采用额外填充的元素(如图 2 所示)。
为什么会这样?我希望 TextView(绿色部分)填充完整的父级(灰色部分),我还注意到它的大小合适,它只是向下移出了底部的屏幕。
任何人都可以帮我解决这个问题吗?
日志视图.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:gravity="top" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/green"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@android:color/darker_gray"
android:src="@android:drawable/ic_media_pause" />
</RelativeLayout>
主要的.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/stateLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/logBox"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/bt_status"
tools:context=".MainActivity" />
<com.canlab.carguard.view.LogView
android:id="@+id/logBox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/carLabel"
android:background="@color/l_blue" />
<TextView
android:id="@+id/driverLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Řidič:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/carLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/driverLabel"
android:text="Auto:"
android:textAppearance="?android:attr/textAppearanceMedium" />
日志视图类
public class LogView extends RelativeLayout {
private final static String TAG = "LogView";
private final LayoutInflater inflater;
private final int maxLines = 100;
private boolean scroll = true;
private final TextView log;
private final ImageButton pauseButton;
private int i = 0;
public LogView(Context context) {
super(context);
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.addView(inflater.inflate(R.layout.logview, null));
pauseButton = (ImageButton) this.findViewById(R.id.imageButton1);
log = (TextView) this.findViewById(R.id.textView1);
initView(context);
this.setScrollBarStyle(SCROLLBARS_INSIDE_OVERLAY);
}
public LogView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.addView(inflater.inflate(R.layout.logview, null));
pauseButton = (ImageButton) this.findViewById(R.id.imageButton1);
log = (TextView) this.findViewById(R.id.textView1);
initView(context);
}
public LogView(Context context, AttributeSet attrs) {
super(context, attrs);
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.addView(inflater.inflate(R.layout.logview,null));
pauseButton = (ImageButton) this.findViewById(R.id.imageButton1);
log = (TextView) this.findViewById(R.id.textView1);
initView(context);
}
private void initView(Context context){
log.setTextSize(15);
log.setTypeface(Typeface.MONOSPACE);
log.setGravity(Gravity.BOTTOM);
log.setMovementMethod(new ScrollingMovementMethod());
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
for(int i = 0 ; i < getChildCount() ; i++){
getChildAt(i).layout(l, t, r, b);
}
}