创建手机页面

来源:互联网 发布:比特精灵 for mac 编辑:程序博客网 时间:2024/06/11 11:34

1.设计思路(实现原理)

1)将准备好的八个图标复制到res/drawable文件夹下

2)创建一个垂直的线性布局,并在线性布局中创建4个相对布局  

3)在相对布局中添加相应的TextView

4)在values文件下创建一个Values XML,命名为style.xml文件,用来存放抽取出来的样式

5)创建values-zh-rCNvalues-en-rUS文件夹,并在文件夹中创建strings.xml文件

2.案例实现

(1)创建“手机信息页面”程序和“PhoneInfo”程序

创建一个名为“手机信息页面”的程序,该程序用于展示手机设置页面的信息。程序界面对应布局文件activity_task1.xml如下所示:


<LinearLayout 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="@android:color/darker_gray"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <RelativeLayout style="@style/h_wrap_content"
        android:layout_marginTop="10dp">
        <TextView
            style="@style/tv_style"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:drawableTop="@drawable/clound"
            android:text="@string/_cloud" />
        <TextView
            style="@style/tv_style"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:drawableTop="@drawable/bluetooth"
            android:text="@string/_bluetooth" />
    </RelativeLayout>
    <RelativeLayout style="@style/h_wrap_content"
        android:layout_marginTop="10dp">
        <TextView
            style="@style/tv_style"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:drawableTop="@drawable/gesture"
            android:text="@string/_gesture" />
        <TextView
            style="@style/tv_style"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:drawableTop="@drawable/gps"
            android:text="@string/_gps" />
    </RelativeLayout>
    <RelativeLayout style="@style/h_wrap_content"
        android:layout_marginTop="10dp">
        <TextView
            style="@style/tv_style"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:drawableTop="@drawable/info"
            android:text="@string/_system_info" />
        <TextView
            style="@style/tv_style"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:drawableTop="@drawable/internet"
            android:text="@string/_internet" />
    </RelativeLayout>
    <RelativeLayout style="@style/h_wrap_content"
        android:layout_marginTop="10dp">
        <TextView
            style="@style/tv_style"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:drawableTop="@drawable/language"
            android:text="@string/_language" />
        <TextView
            style="@style/tv_style"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:drawableTop="@drawable/notifycation"
            android:text="@string/_set_notifycation" />
    </RelativeLayout>
</LinearLayout>


创建一个名为“PhoneInfo”的程序,该程序用于展示手机设置页面的信息。程序界面对应布局文件activity_task2.xml如下所示:

<LinearLayout 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="@android:color/darker_gray"

    android:orientation="vertical"

    <RelativeLayout style="@style/h_wrap_content"

        android:layout_marginTop="10dp">

        <TextView

            style="@style/tv_style"

            android:layout_alignParentLeft="true"

            android:layout_marginLeft="10dp"

            android:drawableTop="@drawable/clound"

            android:text="@string/_cloud" />

        <TextView

            style="@style/tv_style"

            android:layout_alignParentRight="true"

            android:layout_marginRight="10dp"

            android:drawableTop="@drawable/bluetooth"

            android:text="@string/_bluetooth" />

    </RelativeLayout>

    <RelativeLayout style="@style/h_wrap_content"

        android:layout_marginTop="10dp">

        <TextView

            style="@style/tv_style"

            android:layout_alignParentLeft="true"

            android:layout_marginLeft="10dp"

            android:drawableTop="@drawable/gesture"

            android:text="@string/_gesture" />

        <TextView

            style="@style/tv_style"

            android:layout_alignParentRight="true"

            android:layout_marginRight="10dp"

            android:drawableTop="@drawable/gps"

            android:text="@string/_gps" />

    </RelativeLayout>

    <RelativeLayout style="@style/h_wrap_content"

        android:layout_marginTop="10dp">

        <TextView

            style="@style/tv_style"

            android:layout_alignParentLeft="true"

            android:layout_marginLeft="10dp"

            android:drawableTop="@drawable/info"

            android:text="@string/_system_info" />

        <TextView

            style="@style/tv_style"

            android:layout_alignParentRight="true"

            android:layout_marginRight="10dp"

            android:drawableTop="@drawable/internet"

            android:text="@string/_internet" />

    </RelativeLayout>

    <RelativeLayout style="@style/h_wrap_content"

        android:layout_marginTop="10dp">

        <TextView

            style="@style/tv_style"

            android:layout_alignParentLeft="true"

            android:layout_marginLeft="10dp"

            android:drawableTop="@drawable/language"

            android:text="@string/_language" />

        <TextView

            style="@style/tv_style"

            android:layout_alignParentRight="true"

            android:layout_marginRight="10dp"

            android:drawableTop="@drawable/notifycation"

            android:text="@string/_set_notifycation" />

    </RelativeLayout>

</LinearLayout>

2)抽取样式

  由于编写布局文件时,相同控件之间的外边距和宽高都是固定的。因此会产生大量重复的布局代码,为了代码简洁和重复使用可以将相同代码抽取为样式单独放在一个style.xml文件中。style.xml文件如下所示:

<resources>

    <style name="AppBaseTheme" parent="android:Theme.Light">

    </style>

    <style name="AppTheme" parent="AppBaseTheme">

    </style>

    <!--  match_parent  wrap_content-->

    <style name="h_wrap_content">

        <item name="android:layout_width">match_parent</item>

        <item name="android:layout_height">wrap_content</item>

    </style>

     <!-- 宽高都 match_parent -->

    <style name="tv_style">

        <item name="android:layout_width">180dp</item>

        <item name="android:layout_height">90dp</item>

        <item name="android:gravity">center</item>

        <item name="android:paddingTop">8dp</item>

        <item name="android:paddingBottom">8dp</item>

        <item name="android:drawablePadding">5dp</item>

        <item name="android:background">@android:color/white</item>

    </style>

</resources>

3)创建values-zh-rCNvalues-en-rUS文件夹

res目录下创建values-zh-rCNvalues-en-rUS文件夹,并在这两个文件夹下创建相应的strings.xml文件。

values-zh-rCN文件夹下的strings.xml文件如下所示:

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

<resources>

    <string name="app_name">手机信息页面</string>

    <string name="_cloud">云通信</string>

    <string name="_bluetooth">蓝牙</string>

    <string name="_gesture">自定义手势</string>

    <string name="_gps">定位</string>

    <string name="_system_info">系统信息</string>

    <string name="_internet">网络</string>

    <string name="_language">语言设置</string>

    <string name="_set_notifycation">通知栏设置</string>

</resources>

values-en-rUS文件夹下的strings.xml文件如下所示:

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

<resources>

    <string name="app_name">phoneInfo</string>

    <string name="_cloud">Cloud</string>

    <string name="_bluetooth">Bluetooth</string>

    <string name="_gesture">Gesture</string>

    <string name="_gps">Gps</string>

    <string name="_system_info">SystemInfo</string>

    <string name="_internet">Internet</string>

    <string name="_language">Language</string>

    <string name="_set_notifycation">Notifycation</string>

</resources>

4)编写与界面交互的代码

接下来需要在MainActivity中编写与用户交互的逻辑代码,MainActivity对应的代码如下所示:

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

    }

}

3.所需要的图标

4.运行结果图:


0 0
原创粉丝点击