zoukankan      html  css  js  c++  java
  • 【风马一族_Android】Android 前端内容1

     Android 前端内容 

     

    4.1 View 类概述

      4.1.1 关于 View

        //类型说明

        view(视图)指的是用户界面组件的基本构建基块。一个视图占据屏幕上的矩形区域,负责绘图和事件处理。视图是基类的小部件,用来创建交互式用户界面组件 (如按钮、 文本字段等)。

        //参数提供

        

      4.1.2 关于 ViewGroup

        //类型说明

        //参数提供

    4.2 布局

      4.2.1 相对布局 RelativeLayout

        //类型说明

        

        //参数提供

        //代码化说明

    //对main.xml的片段代码讲解

    //对activity_RelativeLayoutActivity.java的片段代码讲解

        //完整代码举例

    imageab.com高清图   

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     tools:context="cn.com.sgmsc.Relative.Activity_RelativeLayoutActivity">
     8 
     9     <TextView
    10         android:id="@+id/label"
    11         android:layout_width="match_parent"
    12         android:layout_height="wrap_content"
    13         android:text="在此处输入:"        />
    14     <!--
    15             android:background="@android:drawable/editbox_background" 设置组件的背景
    16             android:layout_below="@id/label"   先设置相对的属性,再获取所需的ID
    17     -->
    18     <EditText
    19         android:id="@+id/entry"
    20         android:layout_width="match_parent"
    21         android:layout_height="wrap_content"
    22         android:background="@android:drawable/editbox_background"
    23         android:layout_below="@id/label"/>
    24     <Button
    25         android:id="@+id/Ok"
    26         android:layout_width="wrap_content"
    27         android:layout_height="wrap_content"
    28         android:onClick="onClick"
    29         android:layout_below="@id/entry"
    30         android:layout_alignParentRight="true"
    31         android:layout_marginLeft="10dip"
    32         android:text="确定" />
    33     <Button
    34         android:id="@+id/Cancel"
    35         android:layout_width="wrap_content"
    36         android:layout_height="wrap_content"
    37         android:onClick="onClick"
    38         android:layout_toLeftOf="@id/Ok"
    39         android:layout_alignTop="@id/Ok"
    40         android:text="删除" />
    41 </RelativeLayout>
    main.xml
     1 import android.support.v7.app.AppCompatActivity;
     2 import android.os.Bundle;
     3 import android.view.View;
     4 import android.widget.EditText;
     5 import android.widget.Toast;
     6 
     7 public class Activity_RelativeLayoutActivity extends AppCompatActivity {
     8 
     9     private EditText editText;
    10 
    11     @Override
    12     protected void onCreate(Bundle savedInstanceState) {
    13         super.onCreate(savedInstanceState);
    14         setContentView(R.layout.main);
    15 
    16         editText = (EditText) findViewById(R.id.entry);
    17     }
    18 
    19     public void onClick(View view){
    20         switch (view.getId()){
    21             case R.id.Ok:
    22                 Toast.makeText(this,"输入的数据:"+editText.getText(),Toast.LENGTH_SHORT).show();
    23                 break;
    24             case R.id.Cancel:
    25                     if(editText.getText().length()>0)
    26                         Toast.makeText(this,"删除数据",Toast.LENGTH_SHORT).show();
    27                     else
    28                         Toast.makeText(this,"没有数据",Toast.LENGTH_SHORT).show();
    29                 editText.setText("");
    30                 break;
    31             default:
    32                 break;
    33         }
    34     }
    35 }
    activity_RelativeLayout.java

      

      4.2.2 线性布局 LinearLayout

     //类型说明

        

        //参数提供

        //代码化说明

     

    //完整代码举例

    imageab.com高清图

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:orientation="horizontal"
     8     tools:context="cn.com.sgmsc.Line.Activity_LineLayoutActivity">
     9 
    10     <!--
    11            id="@+id/自己取的名字“     所取的名字,必须是唯一,最好存在某种意义,
    12            id="@id/取别人的名字”      例如:在相对布局中,就是获取其它的组件id,来识别相应组件的位置
    13            组件、布局都需要其高度/宽度  wrap_contentr的大小是根据组件中文本大小及数量来决定
    14            match_parent的大小是尽可能的占领空白空间
    15            text="输入所要的文本内容"
    16            layout_weight:表示权重,在线性布局中,权重的大小影响了组件在布局中显示的大及组件间的关系
    17            android:onClick="onClick"   设置该组件在业务逻辑中可以进行事件处理,这一步骤让组件在
    18                                         Activity_LineLayoutActivity.java无需定义组件,就可以直接进行业务逻辑
    19             还有其它参数,没有使用,强烈希望读者可以尝试。
    20     -->
    21     <Button android:id="@+id/button1"
    22         android:layout_width="wrap_content"
    23         android:layout_height="wrap_content"
    24         android:text="Hello, I am a Button1"
    25         android:onClick="onClick"
    26         android:layout_weight="1"      />
    27     <Button android:id="@+id/button2"
    28         android:layout_width="wrap_content"
    29         android:layout_height="wrap_content"
    30         android:text="Hello, I am a Button2"
    31         android:onClick="onClick"
    32         android:layout_weight="1"       />
    33     <Button android:id="@+id/button3"
    34         android:layout_width="wrap_content"
    35         android:layout_height="wrap_content"
    36         android:text="Hello, I am a Button3"
    37         android:onClick="onClick"
    38         android:layout_weight="1"      />
    39     <Button android:id="@+id/button4"
    40         android:layout_width="wrap_content"
    41         android:layout_height="wrap_content"
    42         android:text="Hello, I am a Button4"
    43         android:onClick="onClick"
    44         android:layout_weight="1"       />
    45     <Button android:id="@+id/button5"
    46         android:layout_width="wrap_content"
    47         android:layout_height="wrap_content"
    48         android:text="Hello, I am a Button5"
    49         android:onClick="onClick"
    50         android:layout_weight="1"       />
    51 </LinearLayout>
    main.xml
     1 import android.support.v7.app.AppCompatActivity;
     2 import android.os.Bundle;
     3 import android.view.View;
     4 import android.widget.Toast;
     5 
     6 public class Activity_LineLayoutActivity extends AppCompatActivity {
     7 
     8     @Override
     9     protected void onCreate(Bundle savedInstanceState) {
    10         super.onCreate(savedInstanceState);
    11         //获取布局文件
    12         setContentView(R.layout.main);
    13     }
    14 
    15     //此对象,对应main.xml文件中,组件的android:onClick="onClick"
    16     //此对象,用来对组件进行业务逻辑
    17     public void onClick(View view){
    18         //通过判断组件的Id,来确定相应的组件
    19         switch (view.getId()){
    20             case R.id.button1: //如果是按钮1,则进行业务逻辑
    21                 Toast.makeText(this, "你点击的是按钮 1", Toast.LENGTH_SHORT).show();   //显示屏中弹出提示框
    22                 break;
    23             case R.id.button2://如果是按钮2,则进行业务逻辑
    24                 Toast.makeText(this, "你点击的是按钮 2", Toast.LENGTH_SHORT).show();
    25                 break;
    26             case R.id.button3:
    27                 Toast.makeText(this, "你点击的是按钮 3", Toast.LENGTH_SHORT).show();
    28                 break;
    29             case R.id.button4:
    30                 Toast.makeText(this, "你点击的是按钮 4", Toast.LENGTH_SHORT).show();
    31                 break;
    32             case R.id.button5:
    33                 Toast.makeText(this, "你点击的是按钮 5", Toast.LENGTH_SHORT).show();
    34                 break;
    35             default:
    36                 break;
    37         }
    38     }
    39 }
    Activity_LinearLayout.java

      4.2.3 表格布局 TableLayout

    imageab.com高清图

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <TableLayout
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:stretchColumns="0,1,2"
     8     tools:context=".Activity_TableLayoutActivity">
     9 
    10     <!--
    11      android:stretchColumns    设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。
    12      android:shrinkColumns     设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。
    13      android:collapseColumns    设置要隐藏的列
    14 
    15      TableLayout:表格布局
    16      TableRow : 行,必需给
    17      <TableRow> 所设的组件,都放在同一行
    18            设置组件
    19      </TableRow>
    20     -->
    21 
    22     <TableRow><!-- row1 -->
    23         <Button android:id="@+id/button1"
    24             android:layout_width="wrap_content"
    25             android:layout_height="wrap_content"
    26             android:text="按钮1"
    27             android:onClick="onClick"
    28             android:layout_column="0"            />
    29         <Button android:id="@+id/button2"
    30             android:layout_width="wrap_content"
    31             android:layout_height="wrap_content"
    32             android:text="按钮2"
    33             android:onClick="onClick"
    34             android:layout_column="1"            />
    35     </TableRow>
    36     <TableRow><!-- row2 -->
    37         <Button android:id="@+id/button3"
    38             android:layout_width="wrap_content"
    39             android:layout_height="wrap_content"
    40             android:text="按钮3"
    41             android:onClick="onClick"
    42             android:layout_column="1"          />
    43         <Button android:id="@+id/button4"
    44             android:layout_width="wrap_content"
    45             android:layout_height="wrap_content"
    46             android:text="按钮4"
    47             android:onClick="onClick"
    48             android:layout_column="1"         />
    49     </TableRow>
    50     <TableRow ><!-- row3 -->
    51         <Button android:id="@+id/button5"
    52             android:layout_width="wrap_content"
    53             android:layout_height="wrap_content"
    54             android:text="按钮5"
    55             android:onClick="onClick"
    56             android:layout_column="2"          />
    57     </TableRow>
    58 </TableLayout>
    main.xml
     1 import android.support.v7.app.AppCompatActivity;
     2 import android.os.Bundle;
     3 import android.view.View;
     4 import android.widget.Toast;
     5 
     6 public class Activity_TableLayoutActivity extends AppCompatActivity {
     7 
     8     @Override
     9     protected void onCreate(Bundle savedInstanceState) {
    10         super.onCreate(savedInstanceState);
    11         setContentView(R.layout.main);
    12     }
    13 
    14     public void onClick(View view){
    15         switch (view.getId()){
    16             case R.id.button1:
    17                 Toast.makeText(this,"这个按钮在第一行第一列",Toast.LENGTH_SHORT).show();
    18                 break;
    19             case R.id.button2:
    20                 Toast.makeText(this,"这个按钮在第一行第二列",Toast.LENGTH_SHORT).show();
    21                 break;
    22             case R.id.button3:
    23                 Toast.makeText(this,"这个按钮在第二行第二列",Toast.LENGTH_SHORT).show();
    24                 break;
    25             case R.id.button4:
    26                 Toast.makeText(this,"这个按钮在第二行第三列",Toast.LENGTH_SHORT).show();
    27                 break;
    28             case R.id.button5:
    29                 Toast.makeText(this,"这个按钮在第三行第三列",Toast.LENGTH_SHORT).show();
    30                 break;
    31         }
    32     }
    33 }
    Activity_TableLayout.java

      4.2.4 帧布局 FrameLayout

      

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <FrameLayout
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     tools:context="cn.com.sgmsc.Frame.Activity_FrameLayout">
     8 
     9     <!--
    10         这个帧布局中放入三个按钮,按钮间的位置中叠加式的,处于最底部位置是从先放入的组件开始的,
    11         处于最顶部位置的是最后放入的组件,其它部件的位置以此类推
    12         从界面显示的效果可知
    13     -->
    14     <!--红色-->
    15     <Button
    16         android:layout_width="350sp"
    17         android:layout_height="350sp"
    18         android:layout_gravity="center"
    19         android:background="@color/colorAccent"/>
    20 
    21     <!--蓝色-->
    22     <Button
    23         android:layout_width="250sp"
    24         android:layout_height="250sp"
    25         android:layout_gravity="center"
    26         android:background="@color/colorPrimary"/>
    27 
    28     <!--淡青铜色-->
    29     <Button
    30         android:layout_width="150sp"
    31         android:layout_height="150sp"
    32         android:layout_gravity="center"
    33         android:background="@color/colorPrimaryDark"/>
    34 </FrameLayout>
    main.xml 不含业务处理

    imageab.com高清图

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <FrameLayout
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     tools:context="cn.com.sgmsc.Frame.Activity_FrameLayout">
     8 
     9     <!--
    10         这个帧布局中放入三个按钮,按钮间的位置中叠加式的,处于最底部位置是从先放入的组件开始的,
    11         处于最顶部位置的是最后放入的组件,其它部件的位置以此类推
    12         从界面显示的效果可知
    13     -->
    14     <!--红色-->
    15     <Button
    16         android:id="@+id/Button_red"
    17         android:onClick="onClick"
    18         android:layout_width="350sp"
    19         android:layout_height="350sp"
    20         android:layout_gravity="center"
    21         android:background="@color/colorAccent"/>
    22 
    23     <!--蓝色-->
    24     <Button
    25         android:id="@+id/Button_blue"
    26         android:onClick="onClick"
    27         android:layout_width="250sp"
    28         android:layout_height="250sp"
    29         android:layout_gravity="center"
    30         android:background="@color/colorPrimary"/>
    31 
    32     <!--淡青铜色-->
    33     <Button
    34         android:id="@+id/Button_bronze"
    35         android:onClick="onClick"
    36         android:layout_width="150sp"
    37         android:layout_height="150sp"
    38         android:layout_gravity="center"
    39         android:background="@color/colorPrimaryDark"/>
    40 </FrameLayout>
    main.xml
    1 <?xml version="1.0" encoding="utf-8"?>
    2 <resources>
    3     <color name="colorPrimary">#3F51B5</color>
    4     <color name="colorPrimaryDark">#95de5e</color>
    5     <color name="colorAccent">#FF4081</color>
    6 </resources>
    Color.xml
     1 import android.app.Activity;
     2 import android.graphics.Color;
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.view.View;
     6 import android.widget.Button;
     7 import android.widget.Toast;
     8 
     9 import static android.graphics.Color.BLUE;
    10 
    11 public class Activity_FrameLayout extends Activity {
    12 
    13     private Button button_red,button_blue,button_bronze;
    14 
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.main);
    19 
    20         button_red = (Button) findViewById(R.id.Button_red);
    21         button_blue = (Button) findViewById(R.id.Button_blue);
    22         button_bronze= (Button) findViewById(R.id.Button_bronze);
    23 
    24     }
    25 
    26     public void onClick(View view){
    27             switch (view.getId()){
    28                 case R.id.Button_red:
    29                         button_red.setBackgroundColor(Color.parseColor("#adaf23"));
    30                         button_blue.setBackgroundColor(Color.parseColor("#3F51B5"));
    31                         button_bronze.setBackgroundColor(Color.parseColor("#95de5e"));
    32                      break;
    33                 case R.id.Button_blue:
    34                         button_red.setBackgroundColor(Color.parseColor("#FF4081"));
    35                         button_blue.setBackgroundColor(Color.parseColor("#FF02DE"));
    36                         button_bronze.setBackgroundColor(Color.parseColor("#95de5e"));
    37                     break;
    38                 case R.id.Button_bronze:
    39                         button_red.setBackgroundColor(Color.parseColor("#FF4081"));
    40                         button_blue.setBackgroundColor(Color.parseColor("#3F51B5"));
    41                         button_bronze.setBackgroundColor(Color.parseColor("#2ad5ca"));
    42                     break;
    43             }
    44     }
    45 }
    Activity_FrameLayout.java

      4.2.5 绝对布局 AbsoluteLayout

        绝对布局指的是组件固定在手机界面的某一位置。

        在现代中的智能手机,其屏幕大小各异、像素不统一,并且手机还会继续变化。

        使其组件在某些屏幕上无法按原先设计的界面,正常显示出来。

        说句话题外话,笔者就因一款学习外语的软件,它是固定横界面的。因笔者手机的问题,需要它变换界面的方向,才可以继续使用它。它死活不会让手机界面向另外一边横向,或者纵向。笔者只好弃用它了。对于一款面向用户的软件而言。不被使用,简直没有存在的价值。

        绝对布局已经过时,因此,笔者不再提供事例代码。推荐读者使用其它布局。

        温馨提供:如果没充分的理由,不要把界面、组件写死。

    4.3 组件

        部件(Widget)是为构建用户交互界面提供服务的视图对象。Android和其他开发语言一样,提供一套完整的部件实现。常用的Widget包括TextView、EditText、Button、RadioButton、Checkbox和              ScrollView等。由于它们每一个都可以与用户进行交互,所以把Widget类中的每一个部件称为控件。

      Widget类是View类的子类,Widget内的所有控件都继承了View类中的属性和方法。可以在android.widget包中找到Android提供的控件列表。下面分别进行介绍。

      4.3.1 显示文本(TextView)与 显示图片(ImageView)

        TextView(文本框)是用来向用户显示文本内容的控件。它在android.widget.TextView包中定义,如果在Java类设计中使用它,则需要在相应的代码文件前部加上“import                                 android.widget.TextView;”。

       TextView控件中有很多可以在XML文件中设置的属性,这些属性同样可以在代码中以方法动态声明。常用的属性和对应的方法如表4.8所示。

            表4.8   TextView常用的属性和对应方法说明

     属性             方法             描述 

    android:gravity    setGravity(int)         设置TextView在x轴和y轴方向上的显示方式

    android:height     setHeight(int)         设置TextView的高度,以像素为单位

    android:width      setWidth(int)          设置TextView的宽度,以像素为单位

    android:hint       setHint(int)          当TextView中显示的文本内容为空时,显示的文本提示信息

    android:padding     setPadding(int)        设置TextView中显示文本与其父容器边界的间距

    android:text         setText(CharSequence)    为TextView设置显示的文本内容

    android:textColor   setTextColor(ColorStateList) 设置TextView 的文本颜色

    android:typeface    setTypeface(Typeface)    设置TextView 的文本字体

    android:textSize     setTextSize(float)       设置TextView 的文本大小

    android:textStyle     setTextStyle(TextStyle)     设置TextView 的文本字形,如粗体、斜体等

    在使用中,要注意一些属性的区别。

    1. android:gravity与android:layout_gravity的区别

    android:gravity用于设置这个View内的所有子元素的对齐方式,而android:layout_gravity用于设置这个View在其父容器中的对齐方式。

    2. android:padding与android:layout_margin的区别

    padding是站在父View的角度描述问题的,它规定它里面的内容必须与这个父View边界的距离。而margin则是站在自己的角度描述问题的,规定自己和其他(上下左右)的View之间的距离,如果同一级只有一个View,那么它的效果基本上就和padding一样了。

    1 <TextView
    2         android:layout_width="wrap_content"    //文本框的宽度
    3         android:layout_height="match_parent"   //文本框的高度
    4         android:text="sows"                    //文本的内容
    5         android:textSize="23sp"                //字体的大小
    6         android:textColor="#aacc22"            //字体的颜色
    7         android:gravity="center"/>             //字体相对文本框的位置

      4.3.2 编辑文本框 EditView

      4.3.3 按钮 Button 与 图片按钮 (ImageButton)

      4.3.4 单选按钮(CheckBox)与 多选按钮 (RadioButton)

      4.3.5 模拟时钟(AnalogClock)与 数字时钟 (DigitalClock)

      4.3.6 日期选择(DatePicker)与 时间选择 (TimePicker)

    4.4 UI设计案例

      4.4.1 计算器界面

      4.4.2 掌上微博

    4.5 总结

    4.6 题目

    4.7 设计类读物

    4.8 汉英语对照表

      

      

      

      

    ---恢复内容结束---

    每天完成一件事。 不管是为了什么。
  • 相关阅读:
    C++中的乱七八糟问题
    在Win环境下配置java的环境进行开发步骤
    常用软件破解
    关于QT建立项目中遇到的相关问题的处理办法
    QT5.4.0安装以及与VS2010整合安装---64bit操作系统解决方案
    STL容器之一vector
    STL
    三种初步简易的方法求解数值问题 of C++
    Visual Studio 简单使用常识操作
    江城感怀---诗一首
  • 原文地址:https://www.cnblogs.com/sows/p/5007032.html
Copyright © 2011-2022 走看看