图像显示
图像视图ImageView

效果图:

代码演示:
activity_image_scale.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?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"> <ImageView android:id="@+id/iv_scale" android:layout_width="match_parent" android:layout_height="220dp" android:layout_marginTop="5dp" android:src="@drawable/apple" />
</LinearLayout>
|
java代码中(设置图片时请将xml中android:src=”@drawable/apple” 改为注释)
ImageScaleActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.luowen.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.widget.ImageView;
public class ImageScaleActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_scale); ImageView iv_scale = findViewById(R.id.iv_scale); iv_scale.setImageResource(R.drawable.apple); } }
|
图像视图的缩放形式

默认值时fitCenter
效果图

代码演示:activity_image_scale.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?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"> <ImageView android:id="@+id/iv_scale" android:layout_width="match_parent" android:layout_height="220dp" android:layout_marginTop="5dp" android:src="@drawable/apple" android:scaleType="center" />
</LinearLayout>
|
ImageScaleActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.luowen.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.widget.ImageView;
public class ImageScaleActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_scale); ImageView iv_scale = findViewById(R.id.iv_scale); iv_scale.setImageResource(R.drawable.apple); iv_scale.setScaleType(ImageView.ScaleType.CENTER); } }
|


效果图:

代码演示
activity_image_button.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?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"> <ImageButton android:layout_width="match_parent" android:layout_height="80dp" android:src="@drawable/sqrt" android:scaleType="fitCenter" />
</LinearLayout>
|
ImageButtonActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.luowen.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class ImageButtonActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_button); } }
|
同时展示文字与图片

效果图:

代码演示:
activity_image_text.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <?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:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图标在上" android:drawableTop="@drawable/ic_about" android:gravity="center" android:textSize="18sp" android:background="#FFFFFF" android:padding="5dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图标在下" android:drawableBottom="@drawable/ic_about" android:gravity="center" android:textSize="18sp" android:background="#FFFFFF" android:padding="5dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图标在左" android:drawableLeft="@drawable/ic_about" android:gravity="center" android:textSize="18sp" android:background="#FFFFFF" android:padding="5dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图标在右" android:drawableRight="@drawable/ic_about" android:gravity="center" android:textSize="18sp" android:background="#FFFFFF" android:padding="5dp"/>
</LinearLayout>
|
ImageButtonActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.luowen.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class ImageButtonActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_button); } }
|
Author:
rowen
Permalink:
http://example.com/2025/07/15/Android%E5%9B%BE%E5%83%8F%E6%98%BE%E7%A4%BA/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY?