Android Activity 全屏

用了幾天的SINA 微博後,感覺他的佈局不錯,首先是首頁全屏圖片突出產品預覽,感覺不錯自己也來試驗一把,就一個簡單全屏幕實現過程還真是有很多坑,特記錄下來希望對大家有幫助!

廢話少說,上代碼!

public class TestAgent extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
       this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

實現全屏其實主要就最後兩行代碼,但是程序竟然錯誤,信息如下:

Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:172)
     at android.app.Activity.requestWindowFeature(Activity.java:2719)
意思是 requestFeature 必須在初始化content內容前。我們來看下 API 的說明:

public boolean requestFeature (int featureId)

Since: API Level 1
Enable extended screen features. This must be called before setContentView(). May be called as many times as desired as long as it is before setContentView(). If not called, no extended features will be available. You can not turn off a feature once it is requested. You canot use other title features with FEATURE_CUSTOM_TITLE.
好吧,調整代碼調用順序,把 setContentView() 放到最下面。

怎麼樣,運行成功了,激動,激動。 我使用的是一個ImageView 來作爲首頁的圖片全屏展示,但是發現Activity全屏了,但是圖片沒有全部拉伸

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/welcome"/>
</LinearLayout>
其實也和簡單在ImageView加上:    android:scaleType="fitXY"    就可以了。 效果圖如下:(其實已經看不出來了,看起來就是一張圖片)




 





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章