Android基础知识& Android常用组件|青训营笔记

111 阅读2分钟

这是我参与「第四届青训营 」笔记创作活动的第[3]天

基本UI组件 & 布局

  • 基本UI组件
    • 文本框 TextView

      主要是在界面上面显示文本信息

      <TextView 
          android:id="@+id/textView"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:text="test" />
      
    • 输入框 EidtText

      输入框,用于输入文本

      <EditText
          android:id="@+id/eidtText"
          android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:inputType="text" />
      
    • 按钮 Button

      一个按钮,可以用于点击 触发事件等

      <Button
          android:id="@+id/button"
          android:layout_heigth="wrap_content"
          android:layout_width="wrap_content"
          android:text="按钮" />
      
    • 图片按钮 ImageButton

      图片按钮,显示于图片,但是可以设置点击触发事件

      <ImageButton
          android:id="@+id/imageButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/image" />
      
    • 单选框 RadioButton

      一个单选框,通常都是在RadioGroup中,形成一组单选框,选中了之后就不能取消,一组单选框里面只能有一个单选框被选定

      <RadioGroup
          android:layout_width="march_parent"
          android:layout_height="wrap_conten"
          android:orientation="vertical">
      
          <RadionButton 
              android:id="@+id/radionButton1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="rb1" />
      
           <RadionButton
               android:id="@+id/radionButton2"
               android:layout_width="wrap_content"
               andtoid:layout_height="wrao_content"
               android:text="rb2" />
           />
      </RadioGroup>
      
      
    • 多选框 CheckBox

      每个选项都是独立的,可以选择,也可以取消掉

      <CheckBox
              android:id="@+id/checkBox1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="cb1" />
      <CheckBox
              android:id="@+id/checkBox2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="cb1" />
      
    • 计时器 Chronometer

      一个简易的计时器

      <Chronometer
          android:id="@+id/chronometer"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:format="%s"
      />
      
  • 布局
    • 线性布局 LinearLayout

      • 线性布局具有水平方向与垂直方向的两种布局方式,通过属性android:orientation控制布局方向,默认是水平的方向,horizontal 水平方向, vertical 垂直方向。

      • 可以通过android:layout_weight 如果有两个组件,而且他们的android:layout_weight组件的值都为1,那么这两个组件都占用整个布局的百分之50.

    • 相对布局 RelativeLayout

      • 如果需要嵌套多层的LinearLayout,界面界面就会变成很复杂,会降低渲染速度,而RelativeLayout就能很好的解决,它仅仅只需要一层就可以完成,通常都是RelativeLayout+LinearLayoutweight属性搭配的使用
      • 还可以根据兄弟组件定位,可以将本组件定位在某一个组件的左边或者右边,下方上方等等。
    • 表格布局 TanleLayout

      • TableLayout中,一个则为一行。
      • 可以通过collapseColumns来设置被隐藏的列的序号,shrinkColumns来设置允许被收缩的列的序号,跳过几个等。
    • 帧布局 FrameLayout

      • 这个布局直接在屏幕上开辟出一块空白的区域,当我们往里面添加控件的时候,会默认把他们放到这块区域的左上角。
      • 而这种布局方式没有任何的定位方式,因此应用的场景并不多,帧布局的大小有空间中最大的子控件决定,如果控件的大小一样,那么只能看到最上面的那个组件,后续添加的会覆盖之前的。
    • 网格布局 GridLayout

      • 这个布局可以很方便的设置N行N列,也可以设置线性布局的方向。
      • 可以设置一个组件位于多少行多少列。
    • 约束布局 ConstraintLayout

      • 可以直接在Design上直接拖动设置,可以写代码实现,组要是为了解决布局嵌套过多的问题。
      • 约束布局需要X轴Y轴至少设置一个约束才能控制布局。