初识 Android

创建博客有一年的时间了,一直没把它用起来,颇感惭愧。近日突感有写博客的冲动,更可怕的是这种冲动似乎比我体内的洪荒之力更为凶猛。于是乎,这篇博客悄然诞生。废话不多说,进入正题——初识Android。

  这篇博客从最基本的知识讲起 ..

  1. View(视窗):View分很多种,展示文字的TextView、展示图片的ImageView、显示按钮的Button 等等(内容太多了,不一一列举了)。
  2. Layout(布局): 由屏幕上所有View组成。分为LinearLayout(线性布局)和RelativeLayout(相对布局)。
    • LinearLayout 特有代码:
      android:orientation=horizontal / vertical    //整体布局水平 / 整体布局竖直
      android:layout_weight="1"     //(权重设置为1) 权重越大,空间获得的越多。应先将height(高度)或weight(宽度)设置为 0dp
       ... ...
    • RelativeLayout 特有代码:
      android:layout_alignParentTop="true / false"  //与父视图上缘对齐
      android:layout_alignParentBottom="true / false"  //与父视图下缘对齐
      android:layout_alignParentLeft="true / false"  //与父视图左缘对齐
      android:layout_alignParentRight="true / false"  //与父视图右缘对齐
      android:layout_centerHorizontal="true / false"  //视图水平居中放置
      android:layout_centerVertical="true / false"  //视图垂直居中放置
      
      android:id="@+id/view"    //@指Android app资源 + 资源类型(id) / 视图名
      android:layout_above="@id/view"    //在 ben_text_view 上方
      android:layout_below="@id/view"    //在 ben_text_view 下方
      android:layout_toLeftof="@id/view"    //在 ben_text_view 左边
      android:layout_toRightof="@id/"    //在 ben_text_view 右边

      ... ...

        另外,相对布局向默认将控件添加在屏幕的左上角 <最基本要求:名称不得有任何空格>。

  3. 每个控件必须有宽度和高度。

    例如:

        

  4. IDE:集成开发环境。

  5.  id:指定控件的唯一标识,被保存在R文件中。

  6. 内边距 & 外边距:

    • 内边距:
      android:padding="8dp"    //四周各个边距
      
      细分:
      android:paddingLeft="8dp"    //左边距
      android:paddingRight="8dp"    //右边距
      android:paddingTop="8dp"    //上边距
      android:paddingBottom="8dp"    //下边距

 

    • 外边距:
      android:layout_margin="8dp"    //四周各个边距
      
      细分:
      android:layout_Left="8dp"    //左边距
      android:layout_Right="8dp"    //右边距
      android:layout_Top="8dp"    //上边距
      android:layout_Bottom="8dp"    //下边距

       记住:padding 是在 View 里面加 padding ,而 margin 增加 View 周围的空隙 (padding 是 View 的内边距;margin 是 View 外部的空间)。

  7. 特殊值:match_parent(与父控件等大)、 wrap_content (背景高度宽度自动调整)。

  8. dp (单位.density-independent pixels):密度无关像素 (视图、控件);

    sp (像素值单位):比例无关像素 (只适用于字体) 。

  9. 实现滚动:ScrollView  把所有东西放在 ScrollView 内,并且宽和高是 match_parent 。

    

     注: ScrollView 只可以包含一个项目。

 
原文地址:https://www.cnblogs.com/wq-code/p/6009784.html