Android 自定义视图不填充屏幕?

Android 自定义视图不填充屏幕?,android,android-layout,android-xml,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_par

当我执行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方法,因为它有一堆动画代码。但是,我已经测试了视图边界,只需使用此函数将画布的背景减为白色(因此我知道它不会填充屏幕):


有人有什么想法吗?

奇怪的是,我接受了你的代码,并且能够让它毫无问题地工作。我针对2.2编译了测试,并在2.3.3模拟器上运行。下面以内联方式提供的代码以及此处的邮政编码:

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

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


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

看起来视图应该填满屏幕,但您的自定义onDraw方法可能只绘制了部分视图。你能发布你的onDraw方法的内容吗?@Craigy用一些更相关的信息更新了这个问题。我已经知道标题栏/通知区域,并且在我的清单中有这个主题。它只覆盖了我屏幕的一部分,左右两侧有轻微的黑色填充,底部有很多。如果我今天有时间的话,我可以试着稍后再拍一张截图。
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);
    }
}
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);
    }
}
<?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>