2011-11-05 155 views
3

當我執行我的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); 
} 

任何人只要有任何想法:不過,我只需出白化使用此功能畫布的背景(所以我知道它不是填充屏幕)測試的觀點界限?

+0

它看起來像視圖應該充滿屏幕的問題,但也許你自定義的onDraw方法只有即將它的一部分。你能發佈你的onDraw方法的內容嗎? – Craigy

+0

@Craigy用更多相關信息更新了問題 – onit

回答

0

我設法解決通過確保中的minSdkVersion設定爲至少8

3

奇怪的是,我把你的代碼,並能夠使其工作沒有問題。我將我的測試編譯爲2.2,並在2.3.3模擬器上運行。守則規定內嵌之下,作爲一個zip這裏:

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

我已經知道標題欄/通知區域,並在我的清單中包含了該主題。它只覆蓋了我的屏幕的一部分,右側和左側有很少的黑色填充,底部有很多填充。如果我今天有空,我可能會嘗試再獲得一個截圖。 – onit