您的位置:首页 > 移动开发 > Android开发

【android】初学安卓,简单布局和activity切换

2015-12-15 17:18 423 查看
【android】初学安卓,简单布局和activity切换
Email:chentravelling@163.com
为什么这个时候学安卓呢?当然是因为选修课啊......说好的机器学习暂时就搁浅了,所以就借了本安卓的书开始学习了。

一、环境

Android Studio+JDK 1.8+win 7 64位

为什么选Android Studio呢?感觉很高大上,配置起来也很easy,只是配置模拟器的时候劳心费神了,而且耗内存太刁。言归正传吧!

二、布局

如果学过xml,有过写网页的经验,那么写安卓的界面布局就很easy了。只是,据说android有五大布局:

(1)AbsoluteLayout:直接写控件的x坐标和y坐标,很明显这种方式不被推荐,因为Android系统的硬件分辨率不尽相同,使用这种布局方式无法实现自适应。

(2)RelativeLayout:这个方式就是设置控件相对于参照物的相对位置了,一般灵活吧。

(3)LinerLayout:很方便的布局,使用较多,就是将控件横向或者纵向排列,嵌套起来用就更好了。

(4)FrameLayout:每个控件的左上方都与父容器左上方重叠

(5)GridLayout/TableLayout:经典案例就是用户注册。

TableLayout的特点:

Shrinkable
: 该列的宽度可以进行收缩,以使表格能够适应父容器的大小

Stretchable
: 该列可以进行拉伸,以填满表格中空闲的空间

Collapsed
: 该列将会被隐藏

GridLayout的特点:

android:layout_row
: 固定显示在第几行。

android:layout_column
: 固定显示在第几列,前面几列没控件的话就空着。

android:layout_rowSpan
: 跨几行

android:layout_columnSpan:
跨几列

本来想自己用LinerLayout和TableLayout两种布局方式结合起来写一个注册界面的,唉,只可惜对布局这玩意不是特别来电,怎么弄都有点不好看,为了更快地去学习功能上的知识,于是就先把布局这块放下了,用一个比较简单的界面吧。取名:add_address.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:columnCount="5">
    <TextView
        android:text="手机联系人"/>
    <EditText
        android:hint="姓名"
        android:layout_columnSpan="4"
        android:layout_gravity="fill_horizontal"
        android:id="@+id/name"/>
    <TextView
        android:text="工作单位"/>
    <EditText
        android:layout_columnSpan="4"
        android:layout_gravity="fill_horizontal"
        android:id="@+id/workplace"/>
    <TextView
        android:text="手机号码"/>
    <EditText
        android:layout_columnSpan="4"
        android:layout_gravity="fill_horizontal"
        android:id="@+id/phone"/>
    <TextView
        android:layout_marginTop="10dp"
        android:text="选择性别"/>
    <RadioGroup
        android:layout_marginLeft="10dp"
        android:layout_columnSpan="4"
        android:layout_gravity="fill_horizontal"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/male"
            android:text="男"
            android:checked="true"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <RadioButton
            android:id="@+id/female"
            android:text="女"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
    </RadioGroup>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
        <Button
            android:text="增加"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/add"/>
    </LinearLayout>
</GridLayout>




三、Activity

页面跳转是伴随着不同的activity之间切换而跳转的,不知道这说法有没问题,先看看大概的代码吧:

首先:写AddAddressActivity.java

public class AddAddressActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_address);
    }
}
关键句子:就是setContentView();那么该activity启动后就会打开一个新的界面,也就是我们设计的add_address.xml。接下来就是启动这个activity了,很easy了,再另一个activity里启动就可以了。

(1)一个activity启动另一个activity

这个时候就需要activity之间通信的桥梁Intent:例如从MainActivity启动AddAddressActivity,在MainActivity.java中写

Intent intent = new Intent(MainActivity.this, AddAddressActivity.class);
startActivity(intent);
如果有数据需要传输,那么就需要Bundle运载数据,Bundle在Java里相当于Map~

Bundle bundle = new Bundle();
bundle.putString("name","嘿嘿");
bundle.putInt("age",22);
在接受方,AddAddressActivity.java中:

Bundle bundle = this.getIntent().getExtras();
String name = bundle.getString("name");
int age = bundle.getInt("age");


暂时会这么一些了,接下来再看看SQLite存储~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: