自定义视图不填充屏幕?

时间:2011-11-05 23:37:57

标签: android android-layout android-xml

当我执行我的Android应用程序时,它工作正常,除了视图不会填满屏幕。我正在使用以下xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical"
    > 
    <com.project.name.SplashView
          android:id="@+id/splashView" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
    /> 
</LinearLayout>

我定义了一个自定义视图,扩展了常规视图,我覆盖了onDraw方法以显示启动画面。在代码中,我通过简单的调用设置布局:

setContentView(R.layout.splash);

我不会发布我的onDraw方法,因为它有一堆动画代码。但是,我通过使用这个函数简单地将画布的背景变白来测试视图边界(所以我知道它没有填满屏幕):

private void drawBackground(Canvas c) 
{
    Paint p = new Paint();
    p.setColor(Color.WHITE);
    c.drawRect(0, 0, c.getWidth(), c.getHeight(), p);
}

任何有想法的人?

2 个答案:

答案 0 :(得分:3)

奇怪的是,我接受了你的代码,并且能够毫无问题地使其工作。我针对2.2编译了我的测试,并在2.3.3模拟器上运行它。代码在下面提供,并在此处输入:

ExampleCustomViewFull.zip

屏幕的哪个区域未正确绘制?如果它是标题栏和通知区域,请参阅下面清单中的我的评论:

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.koderutils.example.customviewfull"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="9" />

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <!--
    NOTE: If you want the Activity to take up the entire screen, add this attribute. 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
    -->
    <activity
        android:label="@string/app_name"
        android:name=".ExampleCustomViewFullActivity">
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>

</manifest>

ExampleCustomViewFull.java:

package com.koderutils.example.customviewfull;

import android.app.Activity;
import android.os.Bundle;

public class ExampleCustomViewFullActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }
}

CustomView.java:

package com.koderutils.example.customviewfull;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class CustomView extends View {

    public CustomView(Context context) {
    super(context);
    }

    public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas c) {
    super.onDraw(c);
    Paint p = new Paint();
    p.setColor(Color.WHITE);
    c.drawRect(0, 0, c.getWidth(), c.getHeight(), p);
    }
}

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.koderutils.example.customviewfull.CustomView
    android:id="@+id/custom_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

</LinearLayout>

答案 1 :(得分:0)

我设法通过确保将minSDKVersion设置为至少8来解决问题。