如何保持 android 中图像按钮的宽高比?

2024-02-17

我有 5 个方形 ImageButtons,我想将它们并排排列在屏幕底部。我将每一组(不同的 id)设置为:

        <ImageButton
         android:id="@+id/box1" 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" 
         android:layout_gravity="center"
         android:adjustViewBounds="true"
         android:scaleType="fitXY"
         android:layout_weight="1"
         android:layout_margin="1dp"
         /> 

我在主java中分配了这样的背景:

    int[] imageIds = new int[] {R.id.box1,R.id.box2,R.id.box3,R.id.box4,R.id.box5};
    for(int i = 0; i<5; i++){
        imageButtons[i] = (ImageButton) findViewById(imageIds[i]);
        imageButtons[i].setBackgroundResource(R.drawable.blank);
    }

我想让它做的是缩放宽度以在屏幕底部整齐地并排放置(现在可以了),但高度也会自动缩放以匹配宽度。这可能吗?我不想使用 setImageSource 因为它会在图像按钮周围放置边框。


   <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/layoutButtons">

     <com.package.SquareButton         
        android:layout_height="fill_parent"
        android:layout_width="0dip"          
        android:layout_weight="1"

        <ImageView
        android:id="@+id/box1"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:layout_height="wrap_content"
        android:layout_width="0dp"          
        android:layout_weight="1" 
        android:layout_marginLeft="5dp" 
        android:layout_marginRight="5dp"/>

      </com.package.SquareButton>

      <com.package.SquareButton         
        android:layout_height="fill_parent"
        android:layout_width="0dip"          
        android:layout_weight="1"

        <ImageView
        android:id="@+id/box2"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"          
        android:layout_marginLeft="5dp" 
        android:layout_marginRight="5dp"/>

</com.package.SquareButton>

   .........          
 </LinearLayout>

然后添加这个自定义按钮类:

public class SquareButton extends LinearLayout {

    public SquareButton(Context context) {
        super(context);
    }

    public SquareButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // This is used to make square buttons.
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何保持 android 中图像按钮的宽高比? 的相关文章

  • Firebase 存储 URL 不断随着新令牌的变化而变化

    我正在尝试使用 firebase 数据库和存储构建社交媒体应用程序 以下是预期的流程 用户上传个人资料图片 该图片存储在当前用户文件夹中的 Firebase 存储中 并且 URL 存储在 Firebase 数据库中以便快速访问 工作正常 用
  • 未向 HAL 提供足够的数据,预期位置

    我在 Android Studio 中收到此错误 我只想在按下按钮时打印文本 我收到以下错误 每次按下按钮时都会出现该错误 如果我取消注释掉意图 它也可以正常工作 但是我拥有的代码越多 错误更改就越多 我实际上不确定这是真正的错误 我这么说
  • RecyclerView 滚动不适用于 NestedScrollView

    我有一个布局 其中有一个 NestedScrollView 其中包含一个图像 多个按钮和一个 RecycleView 当我说recyclerView smoothScrollToPosition or recyclerView scroll
  • Facebook SDK 只适用于 Activity 吗?

    我一直在尝试将 Facebook SDK 实现到我的应用程序中 以便让用户通过该应用程序在我们的粉丝墙上发布消息 但是 我什至无法通过 SDK 登录用户 在 SDK 示例中 给出了一个简单的示例 该示例使用Activity尝试使用单点登录授
  • Android - 打开 FB 本机应用程序时未找到处理 Intent 的 Activity

    我正在尝试从通知中收到的网址打开特定帖子上的 FB 应用程序 它在大多数设备上都能完美运行 但在三星 S7 上大多会崩溃 我的简单代码 String url fb post
  • 底页横向问题

    在横向模式下显示底部工作表对话框时 我遇到了错误的行为 该问题出现在24 版本的设计库中 根据下图 底页仅在横向模式下无法正确显示 我正在使用 BottomSheetDialog 类 并且正在遵循本教程 http www skholingu
  • 没有这样的属性:类的variantConfiguration:

    升级 gradle 4 0 0 应用程序构建失败后 出现错误消息 没有这样的属性 类的variantConfiguration com android build gradle internal variant ApplicationVar
  • 当用户拒绝权限时,某些设备上的 onRequestPermissionsResult grantResults 返回空

    In Shot In onRequestPermissionsResult某些设备上的 grantResults 返回空 而某些设备上有一个值PackageManager PERMISSION DENIED当用户拒绝许可时 我已经实现了一个
  • Android MVVM - 如何使 LiveData 发出它拥有的数据(强制触发观察者)

    我有这个ViewModel从网络获取一个列表 然后我填充一个RecyclerView与数据 MyAvailabilityRepository返回一个MutableLiveData 这就是我使用的原因Transformations switc
  • Android 如何使用 facebook 3.0 sdk 点赞帖子

    我想在我的 Android 应用程序中实现 喜欢 选项 但我不知道要使用哪个请求 我打开了一个有效的 Facebook 会话以及我想要点赞的 ID 帖子 我该如何实现这个功能呢 Thanks 我找到了解决方案 为了喜欢帖子 我使用 face
  • 在android API 23中获取用户的位置

    我可以编写获取用户位置的代码 并且在 API 更多细节 我手动启用设备的 GPS 第一次运行应用程序请求权限并且没有日志返回 在下次运行应用程序时 返回我准备好的 Toast 检查您的提供商 这是我写的代码 public class Mai
  • Android 上的自定义视图和窗口属性

    我想要做的是在我的应用程序顶部添加一个视图 该视图类似于过滤器视图 我想操纵屏幕的颜色 并且我还希望能够同时更改屏幕的亮度时间 这两件事似乎是分开起作用的 但不能一起起作用 这是我的代码 添加视图 colourView new Layer
  • 在 Android 上获取一个滚动到 GridView 底部视图的按钮

    我正在尝试将现有的 iPhone 应用程序移植到 Android 我希望有一个按钮滚动到 GridView 底部的视图中 以使用户能够从服务器加载更多数据 目前 我的解决方案只是修复屏幕底部的一个按钮 而不是让它滚动到视图中 这是我的布局代
  • 蓝牙权限在 jetpack compose 中无法正常工作

    我在用com google accompanist accompanist permissions 0 25 1在我的项目中 我正在尝试在运行时请求蓝牙权限 我想知道用户如何知道权限被永久禁用 清单 xml
  • ListView 上的 setOnItemClickListener 不会触发

    我获取 SD 卡的文件列表 并将其显示在 listView 中 就像在自定义适配器的帮助下一样 adapter new ArrayAdapter
  • Android - 框架布局高度与协调器布局不匹配

    我的 FrameLayout 抽屉布局中的容器 有问题 FrameLayout的高度超过了屏幕高度 在底部的android默认菜单按钮下方
  • 使用 Lint 和 SonarQube 分析 Android 项目

    我真的 溢出 了试图让这些东西一起工作 我按照这里的指示进行操作 http docs sonarqube org display PLUG Android Lint Plugin http docs sonarqube org displa
  • Android minSdkVersion 与 Flutter(v2.8.1)

    我已经安装了 firebase 包 据我们所知minSdkVersion 19被推荐 所以 我想做这个过程 但是 我看到这一行包含在android app build gradle file minSdkVersion flutter mi
  • Android Studio gradle 构建时间太长

    My 安卓工作室项目过去构建速度更快 但现在需要很长时间才能构建 有什么想法可能导致延误吗 我努力了https stackoverflow com a 27171878 391401 https stackoverflow com a 27
  • Android 多点触控

    因此 我尝试使用 onTouchEvent 检查多个屏幕触摸 但它似乎仍然只读取第一次触摸 有人可以帮忙吗 这是我的代码 public boolean onTouchEvent MotionEvent e int num e getPoin

随机推荐