[英]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.