Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SDK:更改LinearLayout上的背景色时,按钮变为彩色_Android_Button_Colors_Background_Android Linearlayout - Fatal编程技术网

Android SDK:更改LinearLayout上的背景色时,按钮变为彩色

Android SDK:更改LinearLayout上的背景色时,按钮变为彩色,android,button,colors,background,android-linearlayout,Android,Button,Colors,Background,Android Linearlayout,我现在正在创建的Android应用程序中有一个LinearLayout,其中包含一个渐变灰色的默认按钮。这个线性布局现在是白色的,但当我尝试将背景颜色更改为黄色时,按钮也会变为黄色,这是我不希望发生的。我还尝试使用彩色图像在LinearLayout上设置背景色,但同样的情况也发生了。我如何解决这个问题?代码如下: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap

我现在正在创建的Android应用程序中有一个LinearLayout,其中包含一个渐变灰色的默认按钮。这个线性布局现在是白色的,但当我尝试将背景颜色更改为黄色时,按钮也会变为黄色,这是我不希望发生的。我还尝试使用彩色图像在LinearLayout上设置背景色,但同样的情况也发生了。我如何解决这个问题?代码如下:

  <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:gravity="center"
     android:orientation="horizontal"
     android:background="@color/yellow">

     <Button
        android:id="@+id/buttonCart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"/>
  </LinearLayout>


解决方案:等等,它现在真的可以工作了。当我在手机上运行应用程序时,Eclipse中的按钮在布局视图中变为彩色和透明,而按钮不受影响。我认为当应用程序运行时,它会显示相同的结果。奇怪的是,它在布局视图中变成这样…

在这种情况下,您可以为按钮设置灰色背景色
android:background=“#A4A4A4”

只需单独设置按钮的背景。您可以为按钮的每个状态(按下、聚焦等)使用不同的背景,使其完全定制


为什么背景颜色会随着布局而改变我不确定。听起来您的按钮默认为transparant。检查您的
样式
主题
,确保这不会影响显示内容

你是如何设置按钮的背景的?当按下按钮时,它是否会变为灰色渐变?是的,但如果我这样做,按钮将变得完全扁平和矩形,其大小将被包装为其内部文本的大小。布局的高度也将被包装到最小化按钮。嗯,我可以使用主题AppTheme或AppBaseTheme,但它仍然产生相同的结果。我在styles.xml中找不到任何可能影响外观的特定内容。