【问题标题】:Why button changes size when color changed?为什么当颜色改变时按钮会改变大小?
【发布时间】:2014-03-04 04:22:24
【问题描述】:

我正在编写一个简单的井字游戏应用程序,它有 3 x 3 的按钮阵列,当有人获胜时它会改变颜色,但是当颜色改变时它会变得更大并且您看不到单个按钮?所以我的问题是如何让它们改变颜色但保持相同大小?图片不是附加的千字。下面的代码是不连续的,首先我将按钮初始化为默认状态,然后当我们有获胜按钮时,获胜按钮的颜色会改变,然后在游戏重置时我将按钮恢复为默认状态;请告诉我如何在不超出按钮边界的情况下使按钮颜色变为红色,或者是否有更好的方法来实现整个事情。

button00.setBackgroundResource(android.R.drawable.btn_default);
button00.setBackgroundColor(Color.RED);
button00.setBackgroundResource(android.R.drawable.btn_default);

布局 XML:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

     <Button
        android:id="@+id/Button01"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/Button00"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignBaseline="@+id/Button01"
        android:layout_alignBottom="@+id/Button01"
        android:layout_toRightOf="@+id/Button01"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/Button02"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignBaseline="@+id/Button00"
        android:layout_alignBottom="@+id/Button00"
        android:layout_toRightOf="@+id/Button00"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/Button10"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_below="@+id/Button00"
        android:layout_toLeftOf="@+id/Button00"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/Button11"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignBaseline="@+id/Button10"
        android:layout_alignBottom="@+id/Button10"
        android:layout_toLeftOf="@+id/Button02"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/Button12"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignBaseline="@+id/Button11"
        android:layout_alignBottom="@+id/Button11"
        android:layout_alignLeft="@+id/Button02"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/Button20"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignLeft="@+id/Button10"
        android:layout_below="@+id/Button10"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/Button21"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignBaseline="@+id/Button20"
        android:layout_alignBottom="@+id/Button20"
        android:layout_toLeftOf="@+id/Button12"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/Button22"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignBaseline="@+id/Button21"
        android:layout_alignBottom="@+id/Button21"
        android:layout_alignLeft="@+id/Button12"
        android:text="@string/hello_world" />

         <TextView
            android:id="@+id/testButton"
            android:layout_width="260dp"
            android:layout_height="50dp"
            android:layout_alignLeft="@+id/Button20"
            android:layout_below="@+id/Button20"
            android:text="@string/hello_world" />
     <!-- 
        <Button
            android:id="@+id/resetButton"
            android:layout_width="260dp"
            android:layout_height="90dp"
            android:layout_alignLeft="@+id/testButton"
            android:layout_below="@+id/testButton"
            android:layout_marginTop="25dp"
            android:text="@string/hello_world" /> -->

</RelativeLayout>

【问题讨论】:

  • 你能发布布局xml吗?
  • 已发布,不知道你为什么需要它
  • 为什么要设置后台资源?删除这一行: button00.setBackgroundResource(android.R.drawable.btn_default);然后再试一次。
  • 只要这样做:button00.setBackgroundColor(Color.RED);
  • wqrahd - 这是我要做的事情,因为如果我不这样做,那么在开始新游戏时按钮将保持红色

标签: java android android-layout


【解决方案1】:

在下一行中用setBackgroundResource() 代替Button 背景颜色...

button00.setBackgroundColor(Color.RED);

使用setColorFilter() 作为下面的行...

button00.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);

所以,您更新后的代码将是...

button00.setBackgroundResource(android.R.drawable.btn_default);
button00.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);

【讨论】:

    【解决方案2】:

    您不要尝试更改背景资源您只更改颜色我认为您在更改背景资源时犯了错误尝试在不更改资源的情况下这样做...并执行它我希望它能正常工作..

    【讨论】:

      【解决方案3】:
      > button00.setBackgroundResource(android.R.drawable.btn_default);
      > button00.setBackgroundColor(Color.RED);
      > button00.setBackgroundResource(android.R.drawable.btn_default);
      

      不要使用这个 采用 button00.setImageResource(R.drawable.btn_default);

      【讨论】:

        猜你喜欢
        • 2020-02-15
        • 2015-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多