为什么更改颜色后按钮会更改大小? [英] Why button changes size when color changed?

查看:64
本文介绍了为什么更改颜色后按钮会更改大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a simple tic tac toe application that has 3 by 3 array of buttons, when someone wins it changes colors, however when colors change it becomes of bigger size and you cannot see individual buttons? so my question is how do i make them change color but stay the same size? picture is instead of thousand words attached. the below code is non contiguous, first i initialize button to default, then when we have a winner color of winning buttons will change and then on game reset I return buttons to the default state; please tell me how to make button color red without it going out of button boundaries or if there is a better way to implement the entire thing.

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>

推荐答案

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

Instead of Button background color by setBackgroundResource() in the following line...

button00.setBackgroundColor(Color.RED);

使用setColorFilter()作为下一行...

Use setColorFilter() as the below line...

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

因此,您更新后的代码将是...

So, your updated code will be...

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

这篇关于为什么更改颜色后按钮会更改大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆