【问题标题】:how can i properly keep the aspect ratio on my imagebuttons in android? [duplicate]我怎样才能正确地保持我在 android 中的 imagebuttons 的纵横比? [复制]
【发布时间】:2011-12-19 04:48:54
【问题描述】:

可能重复:
how do i keep the aspect ratio on image buttons in android?

我有 5 个方形骰子作为图像按钮,排列在布局底部附近。我希望它们占据布局的整个宽度,但我似乎无法让它们在占据整个宽度的同时正确保持该纵横比。 目前我的代码如下所示:

 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_margin="1dp"
        android:layout_gravity="bottom" 
        > 
        <ImageButton
             android:id="@+id/die1" 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content" 
             android:layout_gravity="center"
             android:adjustViewBounds="true"
             android:layout_weight="1"
             android:layout_margin="2dp"
             android:scaleType="centerInside"
             />  
        <ImageButton
             android:id="@+id/die2" 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content" 
             android:layout_gravity="center"
             android:adjustViewBounds="true"
             android:layout_weight="1"
             android:layout_margin="2dp"
             android:scaleType="centerInside"
             /> .....

看起来像这样:

如果我不包括

android:layout_height="60dp"

然后骰子会倾斜,看起来像这样:

但是,当我尝试在平板设备或高分辨率的设备上显示时,会出现保留“60dp”部分的问题,它最终看起来像这样:

我哪里错了?!

【问题讨论】:

  • 试试android:layout_height="wrap_content"
  • 这就是给我这个的原因:i.stack.imgur.com/yT5WX.jpg
  • 申请线性布局高度
  • 我不确定你的意思?当我用 android:layout_height="wrap_content" 替换 android:layout_height="60dp" 时,它看起来像上图。
  • 现在 &lt;LinearLayout&gt; 属性是 android:layout_height="60dp" 替换为 android:layout_height="wrap_content"

标签: android aspect-ratio


【解决方案1】:

你将不得不做两件事之一:

1) 提供 ldpi、mdpi、hdpi 和 x-hdpi 版本的骰子图像。将它们放在各自的可绘制文件夹中,其余的将由 android 处理。 在这里了解如何:http://developer.android.com/guide/practices/screens_support.html

或者

2) 手动获取屏幕尺寸并设置图像视图尺寸 java.lang.您可以使用以下方法获取屏幕尺寸:

 Display d = ((android.view.WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
 screenWidth = d.getWidth();

剩下的就看你自己了。祝你好运!

-编辑- 设置 ImageView(或 Java 中任何其他 View 的大小)

 myHeight = screenSize/5; //These are in pixels not dp's
 myWidth = screenSize/5;
 LinearLayout.LayoutParams myLayoutParams = 
               new LinearLayout.LayoutParams(myWidth,myHeight);
 myView.setLayoutParams(myLayoutParams);

希望一切顺利。

【讨论】:

  • 感谢您的回答。 1)我不能让它工作。无论我将图像的分辨率设置为 90 像素还是 900 像素,我都会得到完全相同的比率结果。 2)我有点希望通过xml来做到这一点,并避免通过代码做很多数学,但如果一切都失败了,我会实现这个方法。谢谢
  • 我正在尝试使用方法 2,但是当我尝试通过 java 更改 imageButton 时,似乎没有任何反应。我正在使用类似 myImageButton.setMaxHeight(boxSize);其中 boxSize 是 getWidth 除以 5。有没有更好的命令来调整 imageButton 的大小?
  • 是的。我已将代码添加到帖子中。我已经习惯了,我忘记了我花了多长时间才知道如何在 Java 中设置大小。
【解决方案2】:

试试

 android:layout_height="wrap_content"

&

&lt;LinearLayout&gt; 属性为 android:layout_height="60dp" 替换为 android:layout_height="wrap_content"

【讨论】:

    【解决方案3】:

    将 ImageButtons 的宽度设置为 fill_parent 并使用 scaletype fitStart 用于紧靠左边距的图像,fitEnd 用于右侧的图像。至少就您的示例图像而言,应该做到这一点。如果图像的比例宽度超过屏幕宽度,您可能会遇到一些间距问题,但它应该适合您。

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 2015-05-04
      • 2019-07-17
      • 2022-01-25
      • 2019-11-17
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      相关资源
      最近更新 更多