android自定义Activity显示尺寸
在android应用程序开发中,默认的Activity是铺满整个窗口的,但是在一些特殊情况下我们需要的是没有占据整个窗口的Activity,所以我们只有自己去定义符合自己需求的Activity了。我们以一个实例来展示怎样自定义一个适当尺寸的Activity。
首先我们在res目录下新建一个drawable文件夹,并在里面新建一个Activity的背景框float.xml
float.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffffff" />
<stroke android:width="4dp" color="#000000" />
<corners android:radius="4dp" />
<padding android:bottom="15dp" android:left="15dp" android:right="15dp" android:top="15dp" />
</shape>
然后我们在res/values/下新建属于我们Activity的样式
float_style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.FloatActivity" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/shape</item>
</style>
</resources>
我们再给自己的Activity定义一个属于它的布局文件
layout_float.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="@drawable/pic" />
</RelativeLayout>
我们都知道,新建一个Activity后需要在AndroidManifest.xml文件中进行注册,这里也是同样的需要注册,只是在配置<activity>元素时我们需要引用我们自定义好的样式,下面就是新建的Activity类和AndroidManifest.xml文件的配置:
Activity类
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class FloatActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.float_layout);
}
}
AndroidManifest.xml配置片段:
<activity android:name=".FloatActivity" android:theme="@style/Theme.FloatActivity"></activity>
最终效果图: