简体   繁体   English

为什么在片段中为relativelayout设置backgroundcolor时按钮会更改颜色?

[英]Why button change color when i set a backgroundcolor to the relativelayout in fragment?

this is my fragment 这是我的片段

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="135dp"
    android:background="@color/colorTerracota"
    android:padding="15dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/statetexas"
        android:id="@+id/btnTexas"
        android:width="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/stateyucatan"
        android:id="@+id/btnYucatan"
        android:width="100dp"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/btnTexas"
        android:layout_alignStart="@+id/btnTexas" />


</RelativeLayout>

在此处输入图片说明

I wanted this see as it: 我希望这样看:

在此处输入图片说明

Set your buttons' background color in your XML file: 在XML文件中设置按钮的background color

android:background:"#9E9E9E" // Your color

Their background is set by default to be transparent . 它们的背景默认设置为透明

Create a background selector as below in drawable folder 在drawable文件夹中如下创建背景选择器

drawable - selectablebackground.xml 提拉- selectablebackground.xml

   <?xml version="1.0" encoding="utf-8"?>
       <selector xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:drawable="@color/grey_lightest" android:state_pressed="true"/>
       <item android:drawable="@color/grey_lightest" android:state_focused="true"/>
       <item android:drawable="@android:color/white"/>
    </selector>

Replace colors as you like for pressed and normal states. 根据需要,为按下状态和正常状态替换颜色。 Set this as background to your button 将此设置为背景作为按钮

android:background:"@drawable/selectablebackground"

This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在一个片段中,当在ScrollView中将图像设置为RelativeLayout的背景时,RelativeLayout行为异常 - In a fragment,when an image is set as the background of a RelativeLayout in ScrollView, the RelativeLayout is misbehaving 使用Java代码设置RelativeLayout backgroundColor - Set RelativeLayout backgroundColor using java code Android-将RelativeLayout更改为Fragment - Android - Change RelativeLayout to Fragment 为什么片段的背景设置为某种颜色时工具栏会消失? - Why does the toolbar disappear when the background of the fragment is set to a certain color? 为什么我不能为我的RelativeLayout设置onTouchListener? - Why cant i set the onTouchListener for my RelativeLayout? 创建片段时如何更改父布局的颜色? - How do I change the color of a parent layout when a fragment is created? 当我在 android studio 中的 RelativeLayout 下设置时,按钮未显示在输出屏幕中 - Button is not showing in output screen when i set under RelativeLayout in android studio 更改按钮按下Dialog Fragment中的颜色? - Change button press color in Dialog Fragment? 更改特定RelativeLayout Android的颜色 - Change color of a particular RelativeLayout Android Android RelativeLayout更改颜色onClick - Android RelativeLayout change color onClick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM