Activity的启动界面实现

来源:互联网 发布:mac遮瑕盘怎么用 编辑:程序博客网 时间:2024/06/06 04:57

现在大部分安卓应用在启动的 时候,都会先加载一个图片然后再进入到真正的界面,这个图片一般标有公司的logo,网址,或者其他一些东西,这样一方面除了好看美观,让用户对这个公司印象深刻,另一方面程序可以在图片显示的这段时间内,可以对系统状况进行检测,比如有没有插入SIM卡,有没有SD卡,是否已连上网络,或者预先加载一些程序启动后需要的数据等,(当然,这肯定要先计算完成这些任务大概需要的时间,我们这个程序不涉及这么复杂),废话不多说,进入正题,,明确目的:首先是一打开应用让他显示一个图片,然后延时多少秒进入下一个界面,实现这个功能很简单,定义两个Activity和与之对应的布局文件,下面上代码:

MainActivity代码:

import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.support.v7.app.ActionBarActivity;public class MainActivity extends ActionBarActivity {private final int DEALY = 3000;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                startActivity(new Intent(MainActivity.this, SecondActivity.class));            }        }, DEALY);    }}
与之对应的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/blue"    tools:context=".MainActivity"><ImageView    android:id="@+id/imageView"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/xiaohui"    android:layout_marginLeft="80dp"    android:layout_marginRight="80dp"    /><TextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center"    android:text="www.xbmu.com"    android:layout_alignParentBottom="true"    android:textSize="20dp"/></RelativeLayout>
SecondActivity代码:

import android.os.Bundle;import android.support.v7.app.ActionBarActivity;public class SecondActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);    }}
与之对应的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.administrator.app.SecondActivity">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="我是第二个界面"        android:textSize="30dp"        android:gravity="center"        android:layout_centerInParent="true"/></RelativeLayout>

细心的读者会发现,在MainActivity的布局文件中,我定义了一个TextView用来显示一个网址(网址和图片均来自网络),这里推荐大家直接在图片上制作出网址,这样只要一个ImageView就可以了,但这里为啥没有这样做呢,主要是本人不太会ps,网络上也找不到这样的图片,所以用了一个TextView代替,下面上效果图:



这里可能有读者会问,怎么把下载好的图片放到eclipse或Android  studio中呢,很简单,直接把图片复制,然后粘贴到eclipse或Android  studio中的drawable目录下即可,目前安卓支持的图片格式有JPG,GIF,png等格式,这里还用到了颜色资源,这个颜色资源是我自己建的,在values目录里,新建一个xml文件,添加一些你需要的颜色,搞好之后,最好把这个颜色资源文件单独拿出来,今后其他程序如果需要用到颜色资源,直接复制过来,粘贴到values目录中即可,下面把我的颜色资源贴出来供读者参考:

<?xml version="1.0" encoding="utf-8"?><resources>    <color name="red">#d71345</color>    <color name="blue" >#145b7d</color>    <color name="green" >#45b97c</color>    <color name="yellow" >#ffd400</color>    <color name="pink" >#f05b72</color>    <color name="black" >#13030e</color>    <color name="white" >#fffffb</color>    <color name="gray" >#77787b</color>    <color name="orange" >#f47920</color></resources>

同样的,有兴趣的读者可以建立数组资源,尺寸资源等,
这里可能大家还会遇到一个问题,就是程序启动之后,显示的图片上面有标题栏,这样显得很不好看,怎么去掉标题栏呢,有两种方法第一种是在Java代码中,setContentView(R.layout.activity_main);之前加入requestWindowFeature(Window.FEATURE_NO_TITLE);这个代码就可以去掉标题栏,第二种方法就是在manifest.xml文件中,在你想去掉标题栏的Activity里加上android:theme="@style/Theme.AppCompat.NoActionBar",即可去掉标题栏,因为我的Activity是继承的是ActionBarActivity,所以加的是这句,如果继承的是Activity,那就加上这句android:theme="@style/Theme.AppCompat.NoTitleBar",这里再说一句,如果继承的是ActionBarActivity,用requestWindowFeature(Window.FEATURE_NO_TITLE);是去不掉标题栏的,至少我在测试的时候不行,采用manifest.xml那种方法就可以,
<pre name="code" class="java"><pre name="code" class="java">

0 0