安卓(Android Studio)RadioButton作为底部导航栏时,使用drawableTop图片太大无法显示,使用java代码调节图片大小

当我们在用RadioButton作为底部导航栏时,会遇到从阿里矢量库下载的icon过大,而导致图片直接把内容挤没的情况,但是我们会发现RadioButton这个控件不能调节图片这种情况

这个时候有两种解决方法:

第一种解决方法就是直接通过调整图片资源的大小来实现,提供不同分辨率的图片资源版本,但是如果我们仅仅是打算做个demo,还要去别的软件进行一下图片资源大小的调整,很头大呢.....

这种情况我会使用第二种解决方法:我们一般习惯把图片放入drawable中然后使用@drawable/xx的方式调用我们的图片资源,然后发现这个图片好大,但是使用RadioButton不能更改图片大小啊!!!这里我们可以看到右上角的图片分辨率是200*200

android:drawableTop="@drawable/xxx"

这里直接使用以上代码不做处理的话一个底部导航栏啥也没有了.....其实我们可以把图片资源粘贴到mipmap文件下然后使用

android:drawableTop="@mipmap/xxx"

以上代码,这样使用你的icon,效果很明显图片变小了很多,这是为什么呢?

这是因为在 Android 中,mipmap文件夹主要用于存放应用图标等资源系统会自动根据设备密度选择合适的资源版本,但是即使是这样的情况图片还是大啊怎么办?这时候,我们就需要通过设置CompoundDrawables来对RadioButton中的图片进行调整了,也就是通过java代码来调整下面上代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ActivityBase">

    <RelativeLayout
        android:id="@+id/ly_top_bar"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@drawable/bg">

        <TextView
            android:id="@+id/txt_topbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="信息"
            android:textColor="@color/black"
            android:textSize="18sp" />


        <View
            android:layout_width="match_parent"
            android:layout_height="2px"
            android:layout_alignParentBottom="true"
            android:background="@color/white" />

    </RelativeLayout>


    <RadioGroup
        android:id="@+id/rg_tab_bar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:background="#FBFAFA"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rd_oprate"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:checked="true"
            android:drawableTop="@mipmap/operate"
            android:text="控制"
            android:textColor="@drawable/select_color"
            android:textSize="16sp"/>

        <RadioButton
            android:id="@+id/rd_message"
            android:button="@null"
            android:drawableTop="@mipmap/message"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="16sp"
            android:gravity="center"
            android:textColor="@drawable/select_color"
            android:text="信息" />

        <RadioButton
            android:id="@+id/rd_social"
            android:button="@null"
            android:drawableTop="@mipmap/social"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="16sp"
            android:textColor="@drawable/select_color"
            android:text="社区" />

        <RadioButton
            android:id="@+id/rd_setting"
            android:button="@null"
            android:drawableTop="@mipmap/setting"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="16sp"
            android:textColor="@drawable/select_color"
            android:text="设置" />


    </RadioGroup>

    <View
        android:id="@+id/div_tab_bar"
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:layout_above="@id/rg_tab_bar"
        android:background="@color/white" />


    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/ViewPager2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/div_tab_bar"
        android:layout_below="@id/ly_top_bar">

    </androidx.viewpager2.widget.ViewPager2>

</RelativeLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class ActivityBase extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    RadioButton rd_oprate, rd_message, rd_social, rd_setting;
    RadioGroup rg_tab_bar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        initView();

    }

    private void initView() {

        rd_oprate = findViewById(R.id.rd_oprate);
        Drawable drawable = getResources().getDrawable(R.mipmap.operate);
        drawable.setBounds(0, 0, 90, 90);
        rd_oprate.setCompoundDrawables(null, drawable, null, null);

        rd_message = findViewById(R.id.rd_message);
        Drawable drawable1 = getResources().getDrawable(R.mipmap.message);
        drawable1.setBounds(0, 0, 90, 90);
        rd_message.setCompoundDrawables(null, drawable1, null, null);

        rd_social = findViewById(R.id.rd_social);
        Drawable drawable2 = getResources().getDrawable(R.mipmap.social);
        drawable2.setBounds(0, 0, 90, 90);
        rd_social.setCompoundDrawables(null, drawable2, null, null); 

        rd_setting = findViewById(R.id.rd_setting);
        Drawable drawable3 = getResources().getDrawable(R.mipmap.setting);
        drawable3.setBounds(0, 0, 90, 90);
        rd_setting.setCompoundDrawables(null, drawable3, null, null);
    }


}

这样子就可以了

        rd_setting = findViewById(R.id.rd_setting);
        Drawable drawable3 = getResources().getDrawable(R.mipmap.setting);
        drawable3.setBounds(0, 0, 90, 90);
        rd_setting.setCompoundDrawables(null, drawable3, null, null);

我们来讲一下每行代码是干吗的:

第一行代码都懂是干嘛的我就不说了.

2. Drawable drawable3 = getResources().getDrawable(R.mipmap.setting);

 这一行代码首先通过调用 `getResources()` 方法获取当前活动的上下文的资源对象,然后调用 `getDrawable()` 方法从资源中获取一个 `Drawable` 对象。`R.mipmap.setting` 是一个图片资源的 ID,它指向了一个图片资源。`getDrawable()` 方法用于从资源中获取 `Drawable` 对象。

3. drawable3.setBounds(0, 0, 90, 90);
具体来说,这里的参数 (0, 0, 90, 90) 将 Drawable 对象的边界设置为一个矩形区域,其左上角的坐标为 (0, 0),右下角的坐标为 (90, 90)。这意味着 Drawable 对象的宽度为 90 个单位,高度也为 90 个单位。

设置 Drawable 对象的边界可以影响它在绘制时的大小和位置。在这里,通过设置边界为 (0, 0, 90, 90),即指定了 Drawable 在绘制时的大小为 90x90 像素的矩形区域。这样就控制了 RadioButton 中图标的大小和位置。

4. rd_setting.setCompoundDrawables(null, drawable3, null, null);

setCompoundDrawables() 方法用于设置 RadioButton 的上、下、左、右四个方向的图标,其参数依次表示左、上、右、下四个方向的图标。在这里,我们将 drawable3 设置为 RadioButton 的上方图标,其他三个方向的图标都设置为 null,表示不在这些方向上显示图标。

虚拟机和实机运行后的效果就会变成这样了.是不是很简单呢!

  • 28
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是完整的 Android Studio 代码,包含点击底部导航栏 RadioGroup 有四个 RadioButton 切换对应 ViewPager 包含四个 Fragment 的实现: ``` // 首先在布局文件中添加一个 RadioGroup 和四个 RadioButton <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RadioGroup android:id="@+id/navigation_radio_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:checkedButton="@+id/nav_home"> <RadioButton android:id="@+id/nav_home" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Home" /> <RadioButton android:id="@+id/nav_search" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Search" /> <RadioButton android:id="@+id/nav_notifications" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Notifications" /> <RadioButton android:id="@+id/nav_profile" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Profile" /> </RadioGroup> <androidx.viewpager.widget.ViewPager android:id="@+id/navigation_view_pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/navigation_radio_group" /> </RelativeLayout> ``` 接下来,在对应的 Activity 中找到 RadioGroup 和 ViewPager 并设置它们的监听器: ``` public class MainActivity extends AppCompatActivity { private RadioGroup mNavigationRadioGroup; private ViewPager mNavigationViewPager; private NavigationPagerAdapter mNavigationPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找到 RadioGroup 和 ViewPager mNavigationRadioGroup = findViewById(R.id.navigation_radio_group); mNavigationViewPager = findViewById(R.id.navigation_view_pager); // 设置 ViewPager 的 Adapter mNavigationPagerAdapter = new NavigationPagerAdapter(getSupportFragmentManager()); mNavigationViewPager.setAdapter(mNavigationPagerAdapter); // 设置 RadioGroup 的监听器 mNavigationRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.nav_home: // 切换到 HomeFragment mNavigationViewPager.setCurrentItem(0); break; case R.id.nav_search: // 切换到 SearchFragment mNavigationViewPager.setCurrentItem(1); break; case R.id.nav_notifications: // 切换到 NotificationsFragment mNavigationViewPager.setCurrentItem(2); break; case R.id.nav_profile: // 切换到 ProfileFragment mNavigationViewPager.setCurrentItem(3); break; } } }); } // 定义 ViewPager 的 Adapter private class NavigationPagerAdapter extends FragmentPagerAdapter { public NavigationPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: // 返回 HomeFragment return new HomeFragment(); case 1: // 返回 SearchFragment return new SearchFragment(); case 2: // 返回 NotificationsFragment return new NotificationsFragment(); case 3: // 返回 ProfileFragment return new ProfileFragment(); default: return null; } } @Override public int getCount() { return 4; // 四个 Fragment } } } ``` 最后,定义四个 Fragment,并在对应的布局文件中添加对应的 UI: ``` public class HomeFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home, container, false); // 添加 HomeFragment 的 UI return view; } } public class SearchFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_search, container, false); // 添加 SearchFragment 的 UI return view; } } public class NotificationsFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_notifications, container, false); // 添加 NotificationsFragment 的 UI return view; } } public class ProfileFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_profile, container, false); // 添加 ProfileFragment 的 UI return view; } } ``` 以上就是完整的 Android Studio 代码,包含点击底部导航栏 RadioGroup 有四个 RadioButton 切换对应 ViewPager 包含四个 Fragment 的实现,希望对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值