设置Activity大小不再全屏原理(Activity Dialog)

如何设置Activity的大小,让你的窗口看起来不再是全屏的。有些网友可能知道通过主题比如Theme.Dialog来实现,不过今天Android123告诉大家设置Activity不再全屏显示的原理。Android Theme也主要是通过定义Style来实现的,实现的原理大家可以直接看Android Framework中的定义,今天给一种更简单,但相对灵活的方法,比如不要Theme.Dialog中的边框,下面就一起来看下自定义Activity大小的实现方法。

  1. 创建一个样式文件到你的工程,保存在在res/values/styles.xml,这里文件名不能随便修改,内容为,注意保存时使用UTF-8编码。

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <style name="Theme.Android123" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@drawable/bg</item>
 </style>
</resources>

 2. 上面我们定义的主题风格为Theme.Android123,父风格仍然从Theme.Dialog实现,但我们自定义了背景,位置在drawable/bg中,这里我们创建一个bg.xml文件放到res/drawable文件夹中,bg.xml的内容为

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:Android="http://schemas.android.com/apk/res/android"> 
        <padding android:left="15dp" android:top="15dp" android:right="15dp" android:bottom="15dp" />
        <stroke android:width="3dip" color="#000000" />
        <corners android:radius="5dp" />
 <solid android:color="#ffffff" />      
</shape>

 里面我们定义了一个shape对象,实现背景drawable形状,其中padding代表距离边框,这里我们设置了左、上、右、下四个位置的间距。stroke可以制造出一些3D立体效果,corners是四个角,radisu属性可以设置半径,值越大越圆滑,根据运行效果你可以微调,最后soild是填充颜色,这里我们用了ffffff表示纯白。

  3. 最后在androidmanifest.xml中,在你的activity节点加一个 android:theme属性,值为@style/Theme.Android123 即可。



shape 属于 drawable resource 中的Shape元素

SDK中有如下解释

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

<shape xmlns:android="http://schemas.android.com/apk/res/android" 

    android:shape=["rectangle" | "oval" | "line" | "ring"] > 


    <gradient 

        android:angle="integer" 

        android:centerX="integer" 

        android:centerY="integer" 

        android:centerColor="integer" 

        android:endColor="color" 

        android:gradientRadius="integer" 

        android:startColor="color" 

        android:type=["linear" | "radial" | "sweep"] 

        android:usesLevel=["true" | "false"] /> 

    <solid 

        android:color="color"/> 

    <stroke 

        android:width="integer" 

        android:color="color" 

        android:dashWidth="integer" 

        android:dashGap="integer"/> 

    <padding 

        android:left="integer" 

        android:top="integer" 

        android:right="integer" 

        android:bottom="integer"/> 

    <corners 

        android:radius="integer" 

        android:topLeftRadius="integer" 

        android:topRightRadius="integer" 

        android:bottomLeftRadius="integer" 

        android:bottomRightRadius="integer"/> 

</shape>

<shape>
形状绘制,他是 必须的根元素

属性:

xmlns: 命名空间,必须是 "http://schemas.android.com/apk/res/android". android:shape
Keyword. 定义 Shape的类型。有效的值包括:
ValueDesciption
"rectangle"矩形。默认形状。
"oval"椭圆。
"line"水平直线。需要<stroke>元素定义线的宽度
"ring"环形。

接下来的特性只能在android:shape=”ring”时使用:

android:innerRadius
内环的半径
android:innerRadiusRatio
以环的宽度比率来表示内环的半径。例如,如果android:innerRadiusRatio=”5”,内环半径等于环的宽度除以5。这个值 可以被android:innerRadius覆盖。默认值是9。
android:thickness
环的厚度
android:thicknessRatio         以环的宽度比率来表示环的厚度。 android:useLevel
“true”表示可以当作LevelListDrawable使用。一般都为“false”。
<corners>
为shape创建圆角。当shape是一个矩形时有效。

属性:

              android:radius

                  Dimension。圆角的半径。会被下面的特性覆盖。

              android:topLeftRadius

                  Dimension。左上圆角半径。

              android:topRightRadius

                  Dimension。右上圆角半径。

              android:bottomLeftRadius

                  Dimension。左下圆角半径。

              android:bottomRightRadius

                  Dimension。右下圆角半径。

Note: Every corner must (initially) be provided a corner radius greater than 1, or else no corners are rounded. If you want specific corners to not be rounded, a work-around is to use android:radius to set a default corner radius greater than 1, but then override each and every corner with the values you really want, providing zero ("0dp") where you don't want rounded corners.

<gradient>
为Shape指定渐变色。

属性:

android:angle
渐变色的角度值。0表示从左到右,90表示从下到上。必须是45的倍数,默认是0。
android:centerX
Float. 渐变色中心的X相对位置(0-1.0)。当android:type=”linear”时无效。
android:centerY
Float. 渐变色中心的Y相对位置(0-1.0)。当android:type=”linear”时无效。
android:centerColor
Color. 可选的颜色,出现在start和end颜色之间。
android:endColor
Color. The ending color
android:gradientRadius
Float. 渐变色的半径。当android:type=”radial”时有效。
android:startColor
Color. The starting color
android:type
Keyword. 渐变色的 样式。. 有效值为:
ValueDescription
"linear"线性渐变,默认值。
"radial"环形渐变。start颜色是处于中间的颜色
"sweep"A sweeping line gradient.
android:useLevel
Boolean.“true”表示可以当作LevelListDrawable使用。
<padding>
内部填充

属性:

              android:left

                  Dimension。左内边距。

              android:top

                  Dimension。上内边距。

              android:right

                  Dimension。右内边距。

              android:bottom

                  Dimension。下内边距。

<size>
图形大小

属性:

android:height
android:width
<solid>
填充颜色

属性:

android:color
颜色
<stroke>
线条

属性:

android:width
线的厚度
android:color
线颜色
android:dashGap
间断线间的距离。仅在android:dashWidth设定时有效。
android:dashWidth
间断线的大小。仅在android:dashGap设定时有效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值