用了几天的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);
}
}
实现全屏其实主要就最后两行代码,但是程序竟然错误,信息如下:
意思是 requestFeature 必须在初始化content内容前。我们来看下 API 的说明: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)
好吧,调整代码调用顺序,把 setContentView() 放到最下面。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.
怎么样,运行成功了,激动,激动。 我使用的是一个ImageView 来作为首页的图片全屏展示,但是发现Activity全屏了,但是图片没有全部拉伸
其实也和简单在ImageView加上: android:scaleType="fitXY" 就可以了。 效果图如下:(其实已经看不出来了,看起来就是一张图片)<?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>