android studio ImageView和ImageButton和Button
1.ImageView 1.1代码显示 ImageView img = findViewById(R.id.img); img.setImageResource(R.drawable.apple);
1.2XML
<ImageViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/img"android:src="@drawable/apple"android:scaleType="fitCenter"> </ImageView>
注意:centerinside和center的显示效果居然一模一样,这缘于它们的缩放规则设定,表面上fitCenter、centerinside、center三个类型都是居中显示,目均不越过图像视图的边界。它们之间的区别在于:tCenter既允许缩小图片、也允许放大图片,centerlinside只允许缩小图片、不允许放大图标,而center自始至终保持原始尺寸(既不允许缩小图片、也不允许放大图片),因此,当图片尺寸大于视图宽高,centerinside与fitCenter都会缩小图片,此时它俩的显示效果相同;当图片尺寸小于视图宽高,centerlnside与center都保持图片大小不变,此时它俩的显示效果相同。
2.ImageButton
2.1代码显示
ImageButton imgbtn =findViewById(R.id.imgBtn); imgbtn.setImageResource(R.drawable.apple);
2.2XML
<ImageButtonandroid:id="@+id/imgBtn"android:layout_width="match_parent"android:layout_height="100dp"android:src="@drawable/apple"android:scaleType="fitCenter"> </ImageButton>
3.图片按钮Button
工程想要修改按钮颜色,需要 修改theme
<resources xmlns:tools="http://schemas.android.com/tools"><!-- Base application theme. --><style name="Theme.HTStorage" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge"><!-- Primary brand color. --><item name="colorPrimary">@color/purple_500</item><item name="colorPrimaryVariant">@color/purple_700</item><item name="colorOnPrimary">@color/white</item><!-- Secondary brand color. --><item name="colorSecondary">@color/teal_200</item><item name="colorSecondaryVariant">@color/teal_700</item><item name="colorOnSecondary">@color/black</item><!-- Status bar color. --><item name="android:statusBarColor">?attr/colorPrimaryVariant</item><!-- Customize your theme here. --></style>
</resources>
<Buttonandroid:layout_width="200dp"android:layout_height="80dp"android:text="图片在左"android:drawableLeft="@drawable/wx"android:layout_gravity="center"android:background="#00ff00"> </Button>