布局、样式的使用和程序的国际化

    xiaoxiao2021-03-25  94

           在这一篇博文中,我们主要介绍一下相对布局、线性布局的使用样式的使用以及如何对程序进行国际化。下面通过一个例子来简单介绍一下,我们来做一个手机信息界面,使呈现出如图效果。

     

           首先,在编写一个程序之前我们要有一定的设计思路,才能更好的理解和更快的编出自己想要的程序。

    我的设计思路如下:

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

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

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

        4)在values文件下的style.xml文件中存放抽取出来的样式

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

       有了设计思路之后,我们要开始正式的编程了,具体的实现过程及代码如下:

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

        创建一个名为“手机信息页面”的程序,该程序用于展示手机设置页面的信息。程序界面对应布局文件activity_mian.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"                     //调用自己所设置的样式style,在下文有提到

            android:layout_marginTop="10dp">                                        //指定组件间的距离,距离上端有10dp的距离

            <TextView

                style="@style/tv_style"                                                            //调用自己所设置的样式style

                android:layout_alignParentLeft="true"                                  //因为插入的图片位于左侧,所以需要设置跟父布局左对齐

                android:layout_marginLeft="10dp"                                            //指定组件间的距离,距离左端有10dp的距离

                android:drawableTop="@drawable/clound"                              //使用位于drawable中的图片,使其位于控件的上端位置

                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">                          //AppBaseTheme是指系统自带的样式

        </style>

        <style name="AppTheme" parent="AppBaseTheme">                                           //AppTheme是AppBaseTheme的子类

        </style>

        <!-- match——parent高  wrap_content-->

        <style name="h_wrap_content">                                                                    //style是自己所要设置的样式,命名为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">                                     //style是自己所要设置的样式,命名为tv_style,在上文中有提到

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

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

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

            <item name="android:paddingTop">8dp</item>                         //指定控件的内边距即视图的外边框与内容的距离8dp

            <item name="android:paddingBottom">8dp</item>                  //视图的外边框与内容的距离为8dp

            <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="menu_settings">设置</string>

        <string name="hello_world">你好,世界!</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="menu_settings">Settings</string>

        <string name="hello_world">Hello world!</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>

    <!--在创建values-zh-rCNvalues-en-rUS文件夹时,如果使用Android看的不清晰的话,可以调换到project中去看,如下图所示;在创建strings.xml文件时可以将values文件夹中的strings.xml文件复制过来,修改其中的代码即可-->

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

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

    public class MainActivity extends Activity {                                                      

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    }

    }

      //在程序运行时,比较容易出错的就是这里,因为我们将代码放到了activity_main中,所以我们需要调用的是布局文件是activity_main.xml。

        定义MainActivity类所继承的是Activity类。

    在程序运行出错时,我们还可以使用的方法是:

       点击工具栏中的Build中的Rebuild Project选项,将项目重新构建一下。

    转载请注明原文地址: https://ju.6miu.com/read-15298.html

    最新回复(0)