【问题标题】:RadioGroup button images cut offRadioGroup 按钮图像被截断
【发布时间】:2013-06-15 22:20:55
【问题描述】:

我在底部有一个 RadioGroup,有四个按钮作为我的应用程序的导航(我知道这对于 Android 应用程序来说是非标准的,但这是客户想要的)。它应该是这样的:

这在我的 Nexus 7 上看起来不错,在我的 IDE 中的模拟器和预览中看起来也不错。但是,当我将它加载到 Galaxy S Note 2 上时,它的外观是这样的:

图像被截断,因为对于此特定设备,它们似乎被“移动”到右侧。我有一种感觉,在较小的设备上,它会更加偏斜和截断。当我手动执行大约 35dp 的负左边距时,它实际上看起来很完美,并且它们对齐得很好(但当然该解决方案不适用于其他设备)。

奇怪的是,我似乎并没有在我的代码中做任何特别的事情,而且看起来非常简单。仅供参考,即使我将其更改为仅三个按钮,它仍然将按钮向右移动,而不是真正居中(尽管只有 3 个按钮时它们不会被截断)。我就是想不通它为什么会这样,无论我尝试了多少调整重力、重量甚至换出图像的方法。我目前只对这些使用 /xhdpi 图像,每个图像的大小约为 130x130。

作为参考,下面是我的布局文件中 RadioGroup 的底层代码(它在 RelativeLayout 中):

    <RadioGroup
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tab_radio_group"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:background="@drawable/navigation_base"
            android:paddingTop="15dp">
        <RadioButton
                android:id="@+id/button_scan"
                style="@style/navbar_button"
                android:drawableTop="@drawable/scan"/>
        <RadioButton
                android:id="@+id/button_view_order"
                style="@style/navbar_button"
                android:drawableTop="@drawable/view_order"/>
        <RadioButton
                android:id="@+id/button_complete_order"
                style="@style/navbar_button"
                android:drawableTop="@drawable/complete_order"/>
        <RadioButton
                android:id="@+id/button_settings"
                style="@style/navbar_button"
                android:drawableTop="@drawable/settings"/>
    </RadioGroup>

这里是 navbar_button 的样式元素:

    <style name="navbar_button">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:button">@null</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:layout_weight">1</item>
        <item name="android:textSize">12dp</item>
    </style>

我可能在这里遗漏了一些明显的东西,但是对此有什么想法吗?提前致谢!

【问题讨论】:

  • 我很确定您已经想到了,但是,您尝试过较小的图像吗?此外,设置图标似乎没有被切断。对吗?
  • 我确实尝试将它们重新调整到 100x100 左右,但它仍然会切断图像,即使它在它们之间添加了更多的填充。关于“设置”按钮的好处是,它似乎并没有被切断,尽管它从屏幕上消失了。似乎按钮在屏幕宽度的 25%、50% 和 75% 处被截断。不确定这是否会有所不同,但我觉得值得注意。
  • 你应该有 3 组图像,36x36 ldpi、48x48 mdpi 和 96x96 hdpi dpi
  • 是的,我也试过了。我在 /xhdpi 和 /hdpi 文件夹中的图像是原始尺寸 (130x130),我在 /mdpi 文件夹中添加了 65x65 的图像,在 /ldpi 文件夹中添加了 50x50 的图像。仍然是完全相同的问题,并且看起来相同。 :(
  • 我遇到了完全相同的问题。很讨厌...

标签: android android-layout android-resolution


【解决方案1】:

为了解决这个问题,我最终不得不放弃 RadioGroup 和 RadioButtons,而我使用了带有按钮的简单线性布局。它仍然编写了相对简单的代码,并且只涉及稍微更改 Java 代码以使用普通按钮而不是 RadioButtons。如果有兴趣,这是更改后的布局:

<LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/tab_radio_group"
              android:layout_alignParentBottom="true"
              android:orientation="horizontal"
              android:background="@drawable/navigation_base"
              android:paddingTop="15dp">

    <Button
            android:id="@+id/button_scan"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableTop="@drawable/scan"/>
    <Button
            android:id="@+id/button_view_order"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableTop="@drawable/view_order"/>
    <Button
            android:id="@+id/button_complete_order"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableTop="@drawable/complete_order"/>
    <Button
            android:id="@+id/button_settings"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableTop="@drawable/settings"/>
</LinearLayout>

【讨论】:

    【解决方案2】:

    不确定这是否是唯一的问题,但我发现在函数 setButtonDrawable() 中有对 setMinHeight(mButtonDrawable.getIntrinsicHeight()); 的调用,但没有对 setMinWidth() 的调用,所以我扩展了 RadioButton 类并拥有像这样覆盖函数:

    @Override
    public void setButtonDrawable(Drawable d) {
        super.setButtonDrawable(d);
        if (d != null) {
            setMinWidth(d.getIntrinsicWidth());
            refreshDrawableState();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 2016-09-29
      相关资源
      最近更新 更多