Android阶段学习总结二

Android阶段学习总结二

一个好的软件离不开良好的用户体验和UI布局的美观,Android studio中的页面也是xml文件的格式类似于网页的HTML,Android studio中提供了各种布局与组件,让我们可以根据需求搭建属于自己完美的界面。下面介绍一些我开发中比较常用的布局控件和组件以及消息框。

1、 Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(绝对布局)。那我介绍一下我学习中比较常用的两个布局吧。

(1) LinearLayout(线性布局) 这个布局是我们屏幕适配用得比较多的布局,其中LinearLayout的weight(权重属性)使我们开发过程中用得比较多的一个属性,它可以将LinearLayout 分为若干分,布局或者组件可以根据比例占据相应的屏幕大小。下面通过一个实例来展示吧。

代码如下:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<!—one 权重占1-->
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="one"
       android:textSize="50sp"
       android:textColor="#fff"
       android:background="@color/colorAccent"
       android:layout_weight="1">
   </TextView>
<!—two 权重占2-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="two"
        android:textSize="50sp"
        android:textColor="#fff"
        android:background="@color/colorPrimaryDark"
        android:layout_weight="2">
    </TextView>
<!—three 权重占3-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Three"
        android:textSize="50sp"
        android:textColor="#fff"
        android:background="@color/colorPrimary"
        android:layout_weight="3">
    </TextView>
</LinearLayout>

我这里LinearLayout布局的布局方向是垂直布局,而且三个TextView组件的高度属性是自适应屏幕(layout_height=“wrap_content”)并且第一个(one)TextView所占的权重比例是1,第二个(two)TextView所占的权重比例是2,第三个(three)TextView所占的权重比例是3,所以这三个组件就会根据自己所占的权重来适屏幕,效果如图所示:
在这里插入图片描述

PS;当然需要注意一点的是,当我们把三个TextView组件的高度改为占满父容器(match_parent)的话,然后设置同样的权重比例,此时三个TextView组件的适应屏幕高度就会跟原先的不一样,此时的效果就会像下图一样
在这里插入图片描述

感觉好像少了一个three的TextView了,为什么会这样,这里的需要逻辑运算,我们刚刚说吧三个TextView组件的layout_height=“wrap_parent”(根据内容适应高度) 改为layout_height=“match_parent”(占满父容器的高度);这个时候我们LinerLayout布局里面的三个TextView都想要占满父容器并且还要分配权重,但屏幕只有一个并不能直接分配,所以就要这样的结果1 - 3 = - 2 matchl_parent ;然后把三个TextView
的权重加起来1+2+3=6,所以比例分别为1/6,2/6,3/6;因为大家都是match_parent,所以先到先得分配;所以one的TextView分配结果为1-21/6=2/3,two的TextView分配结果为1-22/6=1/3,由于one和two分配完当前屏幕了,three就无法分配了,占0,所以three不显示出来。
LinerLayou还有更多的其他属性就不详细讲了。

2、 除了布局之外,Android还有很多组件供我们布局使用,下面就简单介绍几个我学习中常用的组件吧;

(1) EditText(文本输入框) 输入框在我们编写Android软件是比较常用的,主要用来获取获取用户输入的数据,获取后提交用户数据,在业务交互中有很重要的作用。EditText的InputType属性可以设置输入框输入数据的限制,使用特定的键盘,优化用户输入。下面列举一些常用的InputType的属性值:
android:inputType=“text” //文本键盘
android:inputType=“textPassword” //密码键盘(输入框密码不可见)
android:inputType=“number” //数字键盘
android:inputType=“numberDecimal” //小数键盘
android:inputType=“phone”//拨号键盘
android:inputType=“datetime” // 时间键盘
android:inputType=“date”//日期键盘
android:inputType=“time”//时间键盘
有了一些键盘限制,对我们输入数据更规范和方便,除此之外我们还可以设置输入框最大输入字符长度maxLength,最大行数maxLines=“3”;selectAllOnFocus属性,当输入框获取到焦点时选中全部内容。

3、 ImageView 图像视图;我们通常使用它来在页面中存放图片,作为图片的载体显示在页面,它也有许多属性。下面简单列举一些主要的属性吧。

src:图像所在的资源路径**
   **ps: src属性和background属性的区别:**

①background通常指的都是背景,而src指的是内容!!
②当使用src填入图片时,是按照图片大小直接填充,并不会进行拉伸
而使用background填入图片,则是会根据ImageView给定的宽度来进行拉伸

scaleType:设置缩放类型

scaleType属性有以下的值可以设置
fitXY:对图像的横向与纵向进行独立缩放,使得该图片完全适应ImageView,但是图片的横纵比可能会发生改变
fitStart:保持纵横比缩放图片,知道较长的边与Image的编程相等,缩放完成后将图片放在ImageView的左上角
fitCenter:同上,缩放后放于中间;
fitEnd:同上,缩放后放于右下角;
center:保持原图的大小,显示在ImageView的中心。当原图的size大于ImageView的size,超过部分裁剪处理。
centerCrop:保持横纵比缩放图片,知道完全覆盖ImageView,可能会出现图片的显示不完全
centerInside:保持横纵比缩放图片,直到ImageView能够完全地显示图片
matrix:默认值,不改变原图的大小,从ImageView的左上角开始绘制原图, 原图超过ImageView的部分作裁剪处理
通过一个实例来展示效果吧;代码如下

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <!--默认缩放比例-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">
        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/pic"
            android:background="#900"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="默认比例"
            android:gravity="center"/>
    </LinearLayout>
    <!--center-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">
        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/pic"
            android:scaleType="center"
            android:background="#900"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="center"
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <!--centerCrop-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">
        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/pic"
            android:background="#900"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="centerCrop"
            android:gravity="center"/>
    </LinearLayout>
    <!--centerInside-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">
        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/pic"
            android:scaleType="centerInside"
            android:background="#900"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="centerInside"
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <!--matrix-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">
        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:scaleType="matrix"
            android:src="@drawable/pic"
            android:background="#900"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="matrix"
            android:gravity="center"/>
    </LinearLayout>
    <!--fitXY-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">
        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/pic"
            android:scaleType="fitXY"
            android:background="#900"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="fitXY"
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>
</LinearLayout>

运行结果如图:
在这里插入图片描述

4、 这里要介绍的是Android用于提示信息的一个控件——Toast(吐司)!,我们平时操作手机经常会在屏幕低下弹出衣蛾暗黑色的弹窗,一段时间后就消失,基本用法:

oast.makeText(MainActivity.this, “提示的内容”,Toast.LENGTH_LONG).show();
下面是一个实例
Xml 文件的代码

<!--按钮-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<Button
    android:id="@+id/btn_show_Toast"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#900"
    android:textSize="30dp"
    android:padding="10dp"
    android:text="弹出Toast"
    android:textColor="#ffff"/>
</LinearLayout>

Java文件的代码

public class TextView extends AppCompatActivity {
    private Button btnshow; //按钮
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_textview);
        //获取控件
        btnshow = findViewById(R.id.btn_show_Toast);
        //设置控件点击事件监听
        btnshow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TextView.this,"你好,欢迎进入",Toast.LENGTH_LONG).show();
            }
        });
    }
}

我们运行代码,启动后点击按钮,消息窗就出来的,显示的结果如图。

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值