#Android学习#android:src=""的用法

本文介绍如何在Android中使用ImageButton,并详细解释了如何通过XML文件设置按钮的不同状态下的显示效果。

2016年 09月 20日 星期二 16:21:50 CST
环境:Android Studio

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_selector"/>
  1. 这是一个图片按钮的部分代码,@drawable/button_selector中的@代表引用,意思就是引用drawable文件夹中的文件;
  2. @指的是 引用系统资源,目标路径framework/base/core/res/res/drawable/*;
  3. android:src=”“中的引用既可以是drawable文件夹下的.png文件,也可以是.xml文件,如下代码片,就是android:src=”@drawable/button_selector”的引用代码:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 指定按钮按钮下时的图片 -->
    <item android:state_pressed="true"
        android:drawable="@drawable/red"
    />
    <!-- 指定按钮松开时的图片 --> 
    <item android:state_pressed="false"
        android:drawable="@drawable/purple"
    />
</selector>
  1. 以上代码可见,android:src= 调用的既可以是drawable文件夹下的.png文件,也可以是.xml文件;
  2. 注意:@只是在.xml文件中使用的;

如果可以,欢迎指出文中的不足
谢谢

仍然是椭圆形 完整代码:<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background_gray"> <!--背景--> <ImageView android:id="@+id/background_on" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/on" android:visibility="visible" /> <!-- 顶部杂项--> <ImageButton android:layout_width="24dp" android:layout_height="24dp" android:layout_margin="16dp" android:background="@android:color/transparent" android:scaleType="centerCrop" android:src="@drawable/back" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="16dp" android:text="Smart Lamp" android:textSize="20dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="46.5dp" > <ImageView android:layout_width="16dp" android:layout_height="16dp" android:layout_gravity="center_vertical" android:src="@drawable/location" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="4dp" android:text="Bedroom" android:textSize="16dp" /> </LinearLayout> <ImageButton android:layout_width="24dp" android:layout_height="24dp" android:layout_gravity="end" android:layout_margin="16dp" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/setting" /> <!-- 按钮组--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="89dp" android:paddingStart="22dp" android:paddingEnd="22dp"> <com.google.android.material.button.MaterialButton android:id="@+id/circularButton" android:layout_width="35dp" android:layout_height="35dp" android:minWidth="35dp" android:minHeight="35dp" android:backgroundTint="#4CAF50" app:cornerRadius="17.5dp" app:strokeColor="@android:color/white" app:strokeWidth="1dp" app:rippleColor="@android:color/transparent" android:clickable="false" android:enabled="false" android:focusable="false" android:contentDescription="圆形绿色按钮" /> </LinearLayout> <!--灯泡背景--> <ImageView android:id="@+id/imageView" android:layout_width="180dp" android:layout_height="244dp" android:layout_gravity="center_horizontal" android:layout_marginTop="207dp" android:src="@drawable/bulbbottom" /> <!--滑动图层--> <ImageView android:id="@+id/lampImageView" android:layout_width="180dp" android:layout_height="244dp" android:layout_gravity="center_horizontal" android:layout_marginTop="190dp" android:adjustViewBounds="true" android:scaleType="centerInside" android:src="@drawable/clip_bulb" /> <TextView android:id="@+id/percentText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="167dp" android:text="0%" android:textColor="#000" android:textSize="18sp" /> <!--灯泡底座--> <ImageView android:layout_width="180dp" android:layout_height="658dp" android:layout_gravity="center_horizontal" android:src="@drawable/bulbonbottom" app:layout_anchor="@+id/background_on" app:layout_anchorGravity="center" /> <!--开关按钮--> <ImageButton android:id="@+id/switch_button" android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="center_horizontal" android:layout_marginTop="476dp" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/switchselector" /> <!--隐藏栏开关--> <!-- <LinearLayout--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="wrap_content"--> <!-- android:layout_marginTop="60dp"--> <!-- android:gravity="center"--> <!-- android:orientation="vertical">--> <!-- <Button--> <!-- android:id="@+id/btn_bottom_sheet"--> <!-- android:layout_width="wrap_content"--> <!-- android:layout_height="wrap_content"--> <!-- android:layout_marginTop="32dp"--> <!-- android:text="BottomSheet"--> <!-- android:textAllCaps="false" />--> </LinearLayout> <!--隐藏toolbar--> <LinearLayout android:id="@+id/ll_bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:behavior_peekHeight="110dp" app:layout_behavior="@string/bottom_sheet_behavior"> <ImageView android:id="@+id/pull" android:layout_width="match_parent" android:layout_height="30dp" android:background="@color/white" android:clickable="false" android:focusable="false" android:gravity="center" android:paddingTop="15dp" android:src="@drawable/pullselector" /> <TextView android:layout_width="match_parent" android:layout_height="80dp" android:background="@color/white" android:gravity="center" android:text="c" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="80dp" android:background="@android:color/holo_orange_dark" android:gravity="center" android:text="b" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="80dp" android:background="@android:color/holo_green_light" android:gravity="center" android:text="c" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="80dp" android:background="@android:color/holo_orange_dark" android:gravity="center" android:text="b" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="80dp" android:background="@android:color/holo_orange_light" android:gravity="center" android:text="c" android:textSize="20sp" /> </LinearLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
最新发布
08-20
根据你提供的完整 XML 布局代码,**`MaterialButton` 显示为椭圆而不是正圆**,最可能的原因是 **父布局 `LinearLayout` 的宽度分配或 `MaterialButton` 自身的内边距(padding)导致宽高比例失衡**。 --- ### ✅ 问题分析 你使用的是: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="89dp" android:paddingStart="22dp" android:paddingEnd="22dp"> <com.google.android.material.button.MaterialButton android:id="@+id/circularButton" android:layout_width="35dp" android:layout_height="35dp" android:minWidth="35dp" android:minHeight="35dp" android:backgroundTint="#4CAF50" app:cornerRadius="17.5dp" app:strokeColor="@android:color/white" app:strokeWidth="1dp" app:rippleColor="@android:color/transparent" android:clickable="false" android:enabled="false" android:focusable="false" android:contentDescription="圆形绿色按钮" /> </LinearLayout> ``` 虽然你设置了 `35dp x 35dp`,但 `MaterialButton` 默认可能会有内部 padding(比如 `MaterialButton` 的 `iconPadding` 或 `internalPadding`),导致实际内容区域小于 35dp,从而看起来是椭圆。 --- ### ✅ 最佳解决方案:使用 `View` 替代 MaterialButton 如果你不需要点击交互功能,建议使用 `View` 实现圆形按钮,更稳定可靠。 #### ✅ 步骤 1:创建 `drawable/circular_button.xml` ```xml <!-- res/drawable/circular_button.xml --> <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="17.5dp" /> <solid android:color="#4CAF50" /> <stroke android:width="1dp" android:color="#FFFFFF" /> </shape> ``` #### ✅ 步骤 2:替换 `MaterialButton` 为 `View` ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="89dp" android:paddingStart="22dp" android:paddingEnd="22dp" android:gravity="center"> <View android:id="@+id/circularButton" android:layout_width="35dp" android:layout_height="35dp" android:background="@drawable/circular_button" android:contentDescription="圆形绿色按钮" /> </LinearLayout> ``` --- ### ✅ 附加建议(如果仍想使用 MaterialButton) 你可以尝试在 `MaterialButton` 中添加以下属性,去除默认 padding: ```xml android:insetTop="0dp" android:insetBottom="0dp" app:iconPadding="0dp" android:padding="0dp" ``` 完整示例如下: ```xml <com.google.android.material.button.MaterialButton android:id="@+id/circularButton" android:layout_width="35dp" android:layout_height="35dp" android:minWidth="35dp" android:minHeight="35dp" android:backgroundTint="#4CAF50" app:cornerRadius="17.5dp" app:strokeColor="@android:color/white" app:strokeWidth="1dp" app:rippleColor="@android:color/transparent" android:clickable="false" android:enabled="false" android:focusable="false" android:contentDescription="圆形绿色按钮" android:padding="0dp" android:insetTop="0dp" android:insetBottom="0dp" app:iconPadding="0dp" /> ``` --- ### ✅ 总结 | 方法 | 优点 | 缺点 | |------|------|------| | 使用 `MaterialButton` | 可交互、样式丰富 | 默认 padding 易导致椭圆 | | 使用 `View` | 稳定显示圆形 | 无法直接响应点击事件(可包裹 `View` 在 `FrameLayout` + `OnClickListener`) | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值