setCompoundDrawables与setCompoundDrawablesWithIntrinsicBounds的区别

本文介绍两种在RadioButton中设置图标的方法:setCompoundDrawablesWithIntrinsicBounds和setCompoundDrawables。第一种方法可保持图标原始比例,而第二种允许手动调整图标大小。

更换radiobutton中的图片在xml中很好设置,但对于初学者如何在代码中设置还是不容易找的。没法子,通过看原版api找到两个方法,setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds。

下面交给大家方法。

第一个方法:setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)

api原文为:

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。图标的宽高将会设置为固有宽高,既自动通过getIntrinsicWidth和getIntrinsicHeight获取。——笔者翻译

1                 button = (RadioButton) group.getChildAt(i);
2                 Resources res = TabTest.this.getResources();
3                 Drawable myImage = res.getDrawable(R.drawable.home);
4                 button.setCompoundDrawablesWithIntrinsicBounds(null, myImage, null, null);

如图第一个按钮:

  

 第二种方法:setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)

api原文为:

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.

意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。但是Drawable必须已经setBounds(Rect)。意思是你要添加的资源必须已经设置过初始位置、宽和高等信息。——笔者翻译

这下就明白了,这个方法要先给Drawable设置setBounds(x,y,width,height);

x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 width:组件的长度 height:组件的高度。

如代码:

1                 Resources res = TabTest.this.getResources();
2                 Drawable myImage = res.getDrawable(R.drawable.home);
3                 myImage.setBounds(1, 1, 100, 100);
4                 button.setCompoundDrawables(null, myImage, null, null);

只要调整好宽和高。效果也是一样的。这个方法的好处就是不按比例,宽高可以打破原有的大小及比例!如图,我调的y轴有点不对齐。

 

总结:radiobutton设置不同方位的图标的方法有以上两种,如果想手动设置大小的话就要用setCompoundDrawables,事先要给Drawable设置setBounds。

        如果按照原有比例大小显示图片就使用setCompoundDrawablesWithIntrinsicBounds

package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.example.myapplication.R; public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener { private Button btnButton, btnButtonLong, btnButtonDisable, btnDrawable; private TextView tvStatus; private boolean isLongButtonEnabled = true; private int drawableState = 0; // 0:无, 1:上, 2:左, 3:下, 4:右 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setupClickListeners(); } private void initView() { btnButton = findViewById(R.id.btn_button); btnButtonLong = findViewById(R.id.btn_button_long); btnButtonDisable = findViewById(R.id.btn_button_disable); btnDrawable = findViewById(R.id.btn_drawable); tvStatus = findViewById(R.id.tv_status); } private void setupClickListeners() { btnButton.setOnClickListener(this); btnButtonLong.setOnClickListener(this); btnButtonLong.setOnLongClickListener(this); btnButtonDisable.setOnClickListener(this); btnDrawable.setOnClickListener(this); } @Override public void onClick(View view) { if (view.getId() == R.id.btn_button) { Toast.makeText(this, "你点击了按钮", Toast.LENGTH_SHORT).show(); updateStatus("基本按钮被点击"); } else if (view.getId() == R.id.btn_button_long) { Toast.makeText(this, "你点击了长按按钮", Toast.LENGTH_SHORT).show(); updateStatus("长按按钮被点击"); } else if (view.getId() == R.id.btn_button_disable) { isLongButtonEnabled = !isLongButtonEnabled; btnButtonLong.setEnabled(isLongButtonEnabled); String status = isLongButtonEnabled ? "长按按钮已启用" : "长按按钮已禁用"; Toast.makeText(this, status, Toast.LENGTH_SHORT).show(); updateStatus(status); } else if (view.getId() == R.id.btn_drawable) { cycleDrawablePosition(); } } @Override public boolean onLongClick(View view) { if (view.getId() == R.id.btn_button_long) { Toast.makeText(this, "你长按了按钮", Toast.LENGTH_SHORT).show(); updateStatus("长按按钮被长按"); return true; } return false; } private void cycleDrawablePosition() { // 清除所有方向的drawable btnDrawable.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); switch (drawableState) { case 0: // 上方 btnDrawable.setCompoundDrawablesWithIntrinsicBounds( 0, R.drawable.doll4, 0, 0 ); updateStatus("图片显示在文字上方"); break; case 1: // 左侧 btnDrawable.setCompoundDrawablesWithIntrinsicBounds( R.drawable.doll4, 0, 0, 0 ); updateStatus("图片显示在文字左侧"); break; case 2: // 下方 btnDrawable.setCompoundDrawablesWithIntrinsicBounds( 0, 0, 0, R.drawable.doll4 ); updateStatus("图片显示在文字下方"); break; case 3: // 右侧 btnDrawable.setCompoundDrawablesWithIntrinsicBounds( 0, 0, R.drawable.doll4, 0 ); updateStatus("图片显示在文字右侧"); break; case 4: // 无图片 updateStatus("图片已隐藏"); break; } drawableState = (drawableState + 1) % 5; } private void updateStatus(String message) { tvStatus.setText(message); } }<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <!-- 基本按钮 --> <Button android:id="@+id/btn_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginVertical="8dp" android:padding="10dp" android:text="按钮" android:textColor="@color/black" android:textSize="20sp" android:textStyle="bold" /> <!-- 长按按钮 --> <Button android:id="@+id/btn_button_long" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginVertical="8dp" android:padding="10dp" android:textAllCaps="false" android:text="长按按钮" android:textColor="@color/black" android:textSize="20sp" android:textStyle="bold" /> <!-- 禁用/启用控制按钮 --> <Button android:id="@+id/btn_button_disable" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginVertical="8dp" android:padding="10dp" android:textAllCaps="false" android:text="点击按钮之后,长按按钮失效;再点击按钮之后,长按按钮有效" android:textColor="@color/black" android:textSize="16sp" android:textStyle="bold" /> <!-- 图片显示按钮 --> <Button android:id="@+id/btn_drawable" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginVertical="8dp" android:drawablePadding="8dp" android:padding="10dp" android:textAllCaps="false" android:text="在按钮里面显示图标" android:textColor="@color/black" android:textSize="16sp" android:textStyle="bold" /> <!-- 状态显示 --> <TextView android:id="@+id/tv_status" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="状态信息将显示在这里" android:textAlignment="center" android:textSize="16sp" android:textColor="@android:color/black" /> </LinearLayout> </ScrollView>展示结果没有做出以下要求:点击按钮在按钮内部文字的上面显示图片, 再点击在按钮内部文字的左侧显示图片, 再点击在按钮内部文字的下侧显示图片, 再点击在按钮内部文字的右侧显示图片, 再点击按钮内部文字的不显示图片。 完成上述业务需要。
最新发布
09-18
评论 10
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值