【问题标题】:Android - Custom Button Color Causes Unwanted ResizeAndroid - 自定义按钮颜色导致不需要的调整大小
【发布时间】:2013-11-19 16:37:51
【问题描述】:

显然改变了这个:

<Button
    android:id="@+id/login"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/register"
    android:background="@color/bluegreen"
    android:text="Log In"
    android:textColor="@android:color/white" />

到这里:

<Button
    android:id="@+id/login"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/register"

    android:text="Log In"
    android:textColor="@android:color/white" />

导致登录按钮缩小(并且添加上面缺少的行会导致更改恢复,所以我确定它与 android:background="@color/bluegreen" 行有关):

示例图片:

http://farm6.staticflickr.com/5519/10947007043_ea9389305d_b.jpg

字符串.xml

<string name="app_name">omv</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="usernamehint">Username</string>
<string name="passwordhint">Password</string>
<color name="grey">#292929</color>
<color name="bluegreen">#43BFC7</color>

【问题讨论】:

  • 请向我们展示完整的代码,然后我可以在我的 IDE 中运行它。

标签: android xml textview android-ui android-button


【解决方案1】:

如果您想增加按钮的大小,请考虑为您的android:layout_height 使用不同的值。下面是一个例子:

<Button
    android:id="@+id/login"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_above="@+id/register"
    android:background="@color/bluegreen"
    android:text="Log In"
    android:textColor="@android:color/white" />

您可以随意使用该值以使其达到您想要的位置。

希望这会有所帮助。祝你好运!

编辑

您的按钮看起来与默认按钮不同的原因是默认使用了一种特殊的图像类型,该类型在图像中构建了填充。此特殊图像称为9patch png,通常用于防止您面临的问题。 ADT 提供了将png 转换为9patch 的工具。标准颜色不会更改为9patch,因此内容如上所示。

【讨论】:

  • @MarcinOrlowski 需要详细说明吗?
  • 我希望它像下面的按钮一样填充父级...这可能吗?
  • 我只是想防止它在更改颜色时缩小 - 没有固定大小。
  • 我可以去掉填充吗?即使我将两个按钮都设置为 android:layout_height="40dp" 它会在按钮的右侧和左侧留下填充,使其看起来比另一个按钮小。我只需要两个按钮看起来完全相同,并且向两者添加 android:layout_height="40dp" 仍然会给我们留下不需要的填充。
  • @user3009687 默认情况下使用背景颜色不使用填充。您在左侧和右侧看到的是使用fill_parent 作为android:layout_width 的值的效果。它拉伸按钮以填充父视图(在本例中是整个屏幕宽度)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
相关资源
最近更新 更多