android studio ImageView和ImageButton和Button

1.ImageView
1.1代码显示
ImageView img = findViewById(R.id.img);
img.setImageResource(R.drawable.apple);

1.2XML

<ImageView
    android: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

<ImageButton
    android: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>
<Button
    android:layout_width="200dp"
    android:layout_height="80dp"
    android:text="图片在左"
    android:drawableLeft="@drawable/wx"
    android:layout_gravity="center"
    android:background="#00ff00">
</Button>

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值