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

Email:[email protected]

为什么这个时候学安卓呢?当然是因为选修课啊......说好的机器学习暂时就搁浅了,所以就借了本安卓的书开始学习了。

一、环境

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>

【android】初学安卓,简单布局和activity切换_第1张图片


三、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存储~

更多相关文章

  1. Android 解决沉浸式状态栏下,输入法弹出,布局不会自动调整的BUG
  2. 布局(一)
  3. android的抽屉控件SlidingDrawer的使用
  4. 浅谈Android五大布局——LinearLayout、FrameLayout, AbsoulteLa
  5. 微软发布 mircosft remote desktop for android
  6. 控件更新Invalidate和postInvalidate的区别
  7. Android自定义View(自定义控件)

随机推荐

  1. Android实现可复用的筛选页面
  2. Android探索:全面分析Activity的生命周期&
  3. android scrollview 滑动到顶端或者指定
  4. android 与C/C++混合编程小例子讲解o
  5. Android入门学习_Android获取来电号码
  6. eclipse ADT插件安装碰到的错误
  7. Android(安卓)Bugly 热修复 快速接入
  8. Android-telephony各文件解释
  9. 中间凹陷的 BottomNavigationView
  10. 十四、ContentProvider往通讯录添加联系