【问题标题】:Fitting image inside a button在按钮内拟合图像
【发布时间】:2014-08-08 12:52:18
【问题描述】:

我的按钮的部分 xml 代码。

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1.1"
        android:orientation="vertical"
        android:weightSum="4" >

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3"
            android:textSize="25sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/button6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6"
            android:textSize="25sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/button9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="9"
            android:textSize="25sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/button12"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:drawableLeft="@drawable/bs2" />
    </LinearLayout>

这里的问题是你看到退格箭头(左箭头)不适合按钮我试图使 .png 文件更大(尝试 48、64、80 和 120)也尝试了重力和 layout_gravity 没有没有得到任何结果。

所以基本上我希望箭头更宽,或者它应该在中间以获得良好的视觉效果。

所以我会喜欢任何有助于不弄乱布局的建议。

【问题讨论】:

  • 您可以使用 ImageButton 代替 Button。
  • @SilentKiller 如果可能的话,我不希望这种情况发生,因为我的按钮不是自定义的,我有点用这个来制作我的主题。有什么办法可以使这项工作?
  • 但在代码中您使用的是Button 默认值。显示您的主题代码。
  • @SilentKiller 是的,按钮是默认的。好吧,我不知何故没想到图像按钮有默认背景:/我仍然不能让按钮像其他按钮一样位于布局中,有什么想法吗?
  • 提供您的箭头图像以及背景图像。'

标签: android xml button


【解决方案1】:

使用 ImageButton 代替按钮并将箭头图像作为按钮的来源

    <ImageButton
        android:id="@+id/id1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow" />

【讨论】:

  • 在调整布局时遇到问题,不像按钮不适合。
【解决方案2】:

你可以使用ImageButton代替Button

要让箭头居中,您应该使用android:scaleType="center"

更新:

当您使用android:drawableLeft="@drawable/bs2" 时,它将仅显示左侧。

如果您想将图像与按钮一起使用,您应该使用 ImageButton,但为此您需要对所有按钮使用两个图像或通用样式。您可以通过以下方式实现。

尝试以下代码。

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/background"
    android:contentDescription="@string/app_name"
    android:scaleType="center"
    android:src="@drawable/back" />

【讨论】:

  • 不,由于我的线性布局,带有背景的“wrap_content”最终使我的布局看起来一团糟。
  • 检查更新的一个@Wince
猜你喜欢
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 2014-12-27
  • 2022-01-04
相关资源
最近更新 更多