andrioid 扩展图片,即点击图片以外的布局区域也响应图片事件

本文提供两种使用XML进行界面布局的方法。方法一展示了如何通过LinearLayout来排列ImageButton和TextView,并设置了背景颜色和居中显示属性。方法二则说明了如何将图片与文字紧密地结合在一起,包括设置TextView的复合图片和调整间距。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="54dip" android:layout_height="match_parent" android:background="#808080" android:gravity="center"> <ImageButton android:id="@+id/btn_effect" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/effect_normal" android:background="@null" android:layout_weight="1" /> <TextView android:id="@+id/text_effect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10sp" android:text="@string/btn_effect_normal"/> <ImageButton android:id="@+id/btn_scene" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/set_scene" android:background="@null" android:layout_weight="1"/> <ImageButton android:id="@+id/btn_size" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@drawable/set_size" android:background="@null" android:layout_weight="1"/> <ImageButton android:id="@+id/btn_focus" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@drawable/set_focus" android:background="@null" android:layout_weight="1"/> <ImageButton android:id="@+id/btn_otherset" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@drawable/set_other" android:background="@null" android:layout_weight="1" /> </LinearLayout>

方法二

当有图片带文字的时候,若想让图片离文字较近,使用如下代码:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="54dip" android:layout_height="match_parent" android:background="#808080" android:gravity="center"> <TextView android:id="@+id/btn_effect" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:textSize="12sp" android:text="@string/video_effect_normal"/> <TextView android:id="@+id/btn_size" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:textSize="12sp" android:background="@null" /> <ImageButton android:id="@+id/btn_balance" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" android:layout_weight="1"/> </LinearLayout>

Drawable draw = getResources().getDrawable(R.drawable.photo); draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight()); BtnEffect.setCompoundDrawablePadding(4); BtnEffect.setPadding(0, 20, 0, 10); BtnEffect.setCompoundDrawables(null, draw, null, null); BtnEffect.setText(mVideoQuality); BtnEffect.setGravity(Gravity.CENTER);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值