Android 界面优化(一)

1. 去除顶部标题栏TitleBar

Android在真机调试时,如果不做特殊设置,每个活动界面的顶部会留有标题栏,如下图所示:

自然标题栏的存在影响了界面整体的美观,解决这一问题的方法主要有以下两种,这里推荐使用第一种方法,第二种方法在刚进入界面时可能会经过一段时间界面才能加载出来。

  • 方法一:设置活动界面的主题Theme

在AndroidManifest清单文件中,可以对每个Activity设定主题样式,例如对于下面的Activity

<activity
    android:theme="@style/splashTheme"
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

设定的主题样式为style样式下的splashTheme(这个主题是自己定义的,用于欢迎界面)。我们到res→values→style.xml文件中可以看到主题splashTheme的定义如下:

<style name="splashTheme" parent="AppTheme">
    <item name="android:windowBackground">@drawable/splash</item>
</style>

(刚才介绍了AndroidManifest清单文件中对Activity主题的设定方式,下面回到我们刚才的问题)

其实我们没有必要一个个的对每个Activity设定主题样式(对某个Activity特殊处理的情况除外),可以在Application标签中统一设定App所有活动界面的主题,如下:

    <application
        android:theme="@style/AppTheme"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:theme="@style/splashTheme"
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
    </application>

要设定Activity无标题栏,只需要在主题样式(这里是AppTheme)中添加标签 <item name="android:windowNoTitle">true</item> 即可实现,此时AppTheme内容如下:

<style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:windowNoTitle">true</item>
    </style>

这时候再运行我们的App,就可以看到想要的效果啦。

  • 方法二:界面加载前请求取消标题栏 (Java代码方式)
protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.activity_main);
}

2. 沉浸式状态栏 (将ActivityLayout背景延伸到状态栏)

实际上仅仅去除标题栏还不是我们最终的目标,我们的目标是将Activity的背景延伸到状态栏,实现沉浸式状态栏,先放效果图。

 

是不是想要的效果?别急,很简单,慢慢来。整体过程分为以下两步:

Step1. 设定ActivityLayout背景

背景可以是动画Animal,也可以是简单的背景图片或颜色填充,直接在布局xml文件根布局中设定background属性即可,这里以图片背景为例。如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background">
</RelativeLayout>

Step2. 设定主题Theme样式

上面已经介绍了如何设定清单文件更改Activity的主题,类似地也可以通过设定主题样式实现沉浸式状态栏,具体的style如下:

<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:fitsSystemWindows">true</item>
</style>

下面对上述主题样式做个简单的说明:

<item name="android:windowNoTitle">true</item> 上面已经介绍了,它的作用是去除Activity的标题栏;

<item name="android:windowContentOverlay">@null</item> 的作用是防止Activity出现白屏,因此设定为无遮盖;

<item name="android:windowTranslucentStatus">true</item> 的作用即为设定状态栏为透明,也就是这里讲到的背景延伸;

<item name="android:fitsSystemWindows">true</item> 的作用是防止控件跑到状态栏上,因此设定为适应屏幕窗口大小。

 

以上是个人在初学Android原生开发时的一些总结,希望对您有所帮助。

联系邮箱:2867555086@qq.com

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值