Android按钮背景图片大小

问题描述 投票:1回答:1

我创建了一个个人键盘,并将背景图像设置为其中一个按钮,但之后按钮大小(与我的背景)不同。

<Button
    android:id="@+id/buttonShift"
    android:paddingTop="0dip"
    android:textSize="22sp"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:textStyle="bold" />

设置背景:

<Button
    android:id="@+id/buttonShift"
    android:background="@drawable/sym_keyboard_shift_off"
    android:paddingTop="0dip"
    android:textSize="22sp"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:textStyle="bold" />

请看按钮的屏幕

之前:

后:

图像大小:106x68

android android-button
1个回答
0
投票

我建议你:

  1. 稍微改变你的drawable - 删除橙色矩形背景,只保留带有透明背景的箭头(从现在起我将使用名称shift_off_arrow作为描述drawable的名称)。透明背景应该只保留整个箭头所需的大小。不大。
  2. 定义一种新颜色 - 橙色背景的颜色(从这一刻开始我假设你已经定义了它,我将使用名称orange_background)
  3. 使用ImageButton而不是Button(就像satnam singh和Hamid Shatu在对你的问题的评论中所说的那样)
  4. 使用这样的代码(使用所有填充来操作以获得完全符合您想要的箭头大小):

<ImageButton
    android:id="@+id/buttonShift"
    android:background="@color/orange_background"
    android:src="@drawable/shift_off_arrow"
    android:paddingTop="0dip"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:layout_weight="1" />
© www.soinside.com 2019 - 2024. All rights reserved.