自定义View元素周围不需要的额外填充

时间:2013-04-05 23:08:45

标签: android view relativelayout padding

!!!问题已解决!!!

在从代码中删除后,问题被覆盖了onLayout函数,一切按预期工作!

我创建了自己的View对象,扩展了RelativeLayout,它显示但是它应用了一些额外的填充(图像低于下) - 灰色区域是添加到主XML布局的自定义元素的大小(按预期大小),绿色是视图按照上面元素的高度以某种方式进行额外填充的元素(如图2所示)。

为什么会这样?我希望TextView(绿色部分)填充整个父级(灰色部分),我也注意到它有适当的大小,它只是从底部的屏幕向下移动。

任何人都可以帮我解决这个问题吗?

Image of screen 1

Image of screen 2

logview.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>

main.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" />

LogView.class

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);

    }
}

0 个答案:

没有答案