【问题标题】:Android ImageButton Resolution ProblemsAndroid ImageButton解析问题
【发布时间】:2012-12-16 18:54:43
【问题描述】:

我想在我的程序顶部设置两个按钮,将用户带到不同的活动。我在格式化时遇到了很多问题。

如何使按钮根据屏幕尺寸按比例拉伸?现在,对于一种屏幕尺寸,它们看起来还不错,然后我将切换到另一种屏幕尺寸,它会显得全部被弄脏或拉伸。我已经尝试了所有不同的 ScaleTypes,但似乎都没有什么不同。我也去了,并使用Shubhayu's answer按比例将所有图像保存到正确的尺寸,如 xhdpi、hdpi 等。

到目前为止,这是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/brushed_metal_background_dark"
tools:context=".HomeActivity" >

<ImageButton
    android:id="@+id/incidentsSelect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/incident_bar2"
    android:contentDescription="Incidents"
    android:onClick="chooseIncident"
    android:scaleType="center" />

<ImageButton
    android:id="@+id/operationalPeriodsSelect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/operationalperiod_bar2"
    android:contentDescription="Operational Periods"
    android:onClick="chooseOperationalPeriod"
    android:scaleType="fitCenter" />

【问题讨论】:

    标签: android layout size resolution imagebutton


    【解决方案1】:

    android:background 更改为android:src,这将保持纵横比。使用android:scaleType="centerInside" 将整个图像放在按钮区域内,并可选择使用android:adjustViewBounds=true 删除空格。示例:

    <ImageButton
        android:id="@+id/incidentsSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:contentDescription="Incidents"
        android:onClick="chooseIncident"
        android:src="@drawable/incident_bar2"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"/>
    
    <ImageButton
        android:id="@+id/operationalPeriodsSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:contentDescription="Operational Periods"
        android:onClick="chooseOperationalPeriod"
        android:src="@drawable/operationalperiod_bar2"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"/>
    

    【讨论】:

    • 谢谢!我还必须使用 android:background="@null" 来摆脱讨厌的背景边框。
    【解决方案2】:

    我猜你正试图在屏幕顶部均匀调整按钮的大小。如果是这种情况,那么您应该设置android:layout_width="0dp"

    【讨论】:

    • 我没有让它们变得均匀的问题,基本上是高度给我带来了问题。我认为它均匀地拉伸了宽度,而不是高度,所以基本上如果屏幕小一点,我的按钮又大又胖,如果它更大,它们会变得很瘦。
    【解决方案3】:

    只需使用 android:layout_width = "wrap_content"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多