您的位置:首页 > 移动开发 > Android开发

android自定义Activity窗口大小

2012-04-19 14:05 337 查看
正常情况下,我们开发的应用程序都会上占满整个屏幕,那么怎么样才能开发出自定义窗口大小的的程序呢?如下图所示:


(转自:http://www.ideasandroid.com/archives/339)

第一步,创建一个背景配置文件float_box.xml,放到res/drawable下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp" android:right="10dp"
android:bottom="10dp" />
<solid android:color="#ffffff" />
<stroke android:width="3dp" color="#000000" />

</shape>

这里conrners是设定圆角的,radius是4个角统一的弧度半径,padding是填充域,这里不是设置shape的填充域,而是设置里面content的填充域,solid是颜色,填充shape域的颜色,stroke是画笔,类似于paint,这里设置线粗和颜色,这些都是api里的,自己可以去查查看!

第二步,定义一个对话框样式,放到res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- 定义一个样式,继承android系统的对话框样式 android:style/Theme.Dialog -->
<style name="Theme.FloatActivity" parent="android:style/Theme.Dialog">

<!-- float_box为我们定义的窗口背景 -->
<item name="android:windowBackground">@drawable/float_box</item>
</style>

</resources>

这里定义一个style变量,名字是Theme.FloatActivity,继承android的Dialog,引用float_box的shape作为背景。

第三步,创建一个视图配置文件res/layout/float_activity.xml,一个ImageView和一个TextView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
android:id="@+id/ideasandroidlogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="@drawable/ideasandroid" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ideasandroidlogo"
android:text="@string/ideasandroidIntr"
android:textColor="@android:color/black" />

</RelativeLayout>

就是一个相对布局,一个ImageView一个TextView;

第四步创建我们的Activity:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 先去除应用程序标题栏 注意:一定要在setContentView之前
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 将我们定义的窗口设置为默认视图
setContentView(R.layout.float_activity);

}
这里去除标题,之前的帖子里说过了,然后设置视图为float_activity就行了!

最后一步,更改应用程序配置文件AndroidManifest.xml:


<activity android:name=".WindowActivityActivity" android:theme="@style/Theme.FloatActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


这样就可以运行了我在写的时候,出现那个@drawable spell错误,直接去window—>preferences—>General—>editors—>text editors—>spelling,把enable spell checking取消掉!网上这么说的!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: