android基础学习【二、基础控件(ImageView、ProgressBar、Notification)】

常用控件

ImageView
       <ImageView
           android:src="@drawable/nowifi"
           android:scaleType="fitXY"
           android:layout_width="200dp"
           android:layout_height="100dp"/>

ctrl + B查看定义

            <enum name="matrix" value="0" />
            <!-- 不改变原图大小,从左上方开始绘图 -->
            <enum name="fitXY" value="1" />
            <!-- 对图像宽高进行独立缩放 -->
            <enum name="fitStart" value="2" />
            <!-- 保持宽高比缩放图片,直到较长的边与图片边长相等 -->
            <enum name="fitCenter" value="3" />
            <!-- 缩放后位于中间 -->
            <enum name="fitEnd" value="4" />
            <!-- 缩放后位于右下角 -->
            <enum name="center" value="5" />
            <!-- Scale the image uniformly (maintain the image's aspect ratio) so both dimensions
                 (width and height) of the image will be equal to or larger than the corresponding
                 dimension of the view (minus padding). The image is then centered in the view. -->
            <enum name="centerCrop" value="6" />
            <!-- Scale the image uniformly (maintain the image's aspect ratio) so that both
                 dimensions (width and height) of the image will be equal to or less than the
                 corresponding dimension of the view (minus padding). The image is then centered in
                 the view. -->
            <enum name="centerInside" value="7" />
            <!-- 
				保持比例缩放,直到可以完全显示图片
			/>

PS:保持图像不变形,并且ImageView与图像宽高相同:

        <ImageView
            android:src="@drawable/nowifi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxHeight="100dp"
            android:maxWidth="100dp"
            android:adjustViewBounds="true"
            />
ProgressBar
public class MainActivity extends AppCompatActivity {
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	progressBar = findViewById(R.id.pb);
    	//点击事件
        msgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setVisibility(View.GONE);
                Log.e("test:","Onclick");
            }
        });
    }
}

PS:inner class 性质:上图使用局部内部类

final: Final用于修饰类、成员变量和成员方法。final修饰的类,不能被继承(String、StringBuilder、StringBuffer、Math,不可变类),其中所有的方法都不能被重写(这里需要注意的是不能被重写,但是可以被重载,这里很多人会弄混),所以不能同时用abstract和final修饰类(abstract修饰的类是抽象类,抽象类是用于被子类继承的,和Final起相反的作F用);Final修饰的方法不能被重写,但是子类可以用父类中Final修饰的方法;

匿名内部类访问局部变量时需要加上final修饰,因为内部类只拷贝了局部变量的值(由构造器完成),用final修饰后,对引用变量来说是引用地址的一致性,对基本类型来说就是值的一致性。例如,对于引用变量,其地址值不能改变,所以这个引用变量就无法再指向其它对象了。

Notification

NotificationManager 是一个重要的系统级服务,该对象位于应用程序的框架层中,应用程序可以通过它向系统发送全局的通知,它是管理Notification的。

这个时候需要创建一个 Notification 对象,用于承载通知的内容。

但是我们一般在实际使用过程中,不会直接去构建 Notification 对象,而是使用它的一个内部类(Android 3.0分水岭):
Android 8.0之后需要创建通知通道。

NotificationCompat.Builder 来实例化一个对象(Android3.0之后) — As old as API Level 4

Notification.Builder 来实例化一个Notification对象。

当获得这个对象之后,可以使用 NotificationManager.notify() 方法发送通知。

    protected void onCreate(Bundle savedInstanceState) {
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //版本判断,Android 8.0
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("kkk","test",NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
        Notification notification = new NotificationCompat.Builder(this,"kkk")
                .setContentTitle("notification")
                .setContentText("there is notification")
                .build()
                ;
    }
     
    public void sendNotification(View view){
        manager.notify(1,notification);
    }
     
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值