安卓学习 之 图片控件和图片按钮

今天学习的是

复制代码
ImageView               和                ImageButton

这两个控件还是比较简单的:先来看看最后的样式图片吧:

从图片中可以看到ImageView中的图片要大很多,这是因为中的ImageView中的图片跟ImageView控件的大小而自动调整。

复制代码
ImageButton中的图片却小很多,因为这里面的图片没有跟随父容器的大小而做出调整,只是展示最原始的大小。

这两个控件中还有两个属性要理解了:

第一个就是 android:src="@mipmap/wether" 这其中的src 是前景的意思 @的意思是:在...里面 整句代码的意思就是 前景是在mipmap文件夹中的wether文件 这样就显示了wenther这个图片

这个图片缩放的位置是:

我的Android Studio安装在了E盘的Android文件夹里面了,所以我的图片资源都放在这个目录下:

E:\Android\release\code\app\src\main\res\mipmap-hdpi

好了今天的布局代码是:

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ButtonActivity"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10sp"
        android:text="按钮1"
        android:id="@+id/btn1"
        android:textSize="36sp"
        android:textColor="#FF0000"
        android:onClick="myclick"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10sp"
        android:text="按钮2"
        android:id="@+id/btn2"
        android:textSize="36sp"
        android:textColor="#FF0000"
        android:onClick="myclick"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10sp"
        android:text="按钮3"
        android:id="@+id/btn3"
        android:textSize="36sp"
        android:textColor="#FF0000"
        android:onClick="myclick"/>

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="36sp"
        android:textColor="#FF0000"
        android:layout_margin="30sp"
        android:background="#00FFFF"/>

    <ImageView
        android:layout_width="200dp"
        android:layout_height="120dp"
        android:src="@mipmap/wether"
        android:tooltipText="天气图片"
        android:background="#00F0FF"/>
    <ImageButton
        android:layout_width="200dp"
        android:layout_height="120dp"
        android:src="@mipmap/wether"
        android:background="#ff00ff"/>



</LinearLayout>

今天的ButtonActivity的文件内容是:

java 复制代码
package com.example.android1;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class ButtonActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_button);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


    }
    public void myclick(View a){
        int id = a.getId();
        TextView text = findViewById(R.id.text1);
        if(id == R.id.btn1){
            Log.e("tag","按钮1被点击了");
            text.setText("按钮1被点击了");
        }
        else if(id == R.id.btn2){
            Log.e("tag","按钮2被点击了");
            text.setText("按钮2被点击了");
        }
        else if(id == R.id.btn3){
            Log.e("tag","按钮3被点击了");
            text.setText("按钮3被点击了");
        }
    }
}
相关推荐
近津薪荼4 分钟前
dfs专题5——(二叉搜索树中第 K 小的元素)
c++·学习·算法·深度优先
_F_y11 分钟前
C++重点知识总结
java·jvm·c++
打工的小王12 分钟前
Spring Boot(三)Spring Boot整合SpringMVC
java·spring boot·后端
毕设源码-赖学姐13 分钟前
【开题答辩全过程】以 高校体育场馆管理系统为例,包含答辩的问题和答案
java·spring boot
lxysbly14 分钟前
n64模拟器安卓版带金手指2026
android
我真会写代码15 分钟前
SSM(指南一)---Maven项目管理从入门到精通|高质量实操指南
java·spring·tomcat·maven·ssm
vx_Biye_Design15 分钟前
【关注可免费领取源码】房屋出租系统的设计与实现--毕设附源码40805
java·spring boot·spring·spring cloud·servlet·eclipse·课程设计
DN金猿21 分钟前
接口路径正确,请求接口却提示404
java·tomcat
敏叔V58741 分钟前
AI智能体的工具学习进阶:零样本API理解与调用
人工智能·学习
Maynor9961 小时前
OpenClaw 玩家必备:用 AI 自动追踪社区最新动态
java·服务器·人工智能