安卓学习笔记-控件-button(按钮背景颜色设置不生效问题)

内容

button继承了TextView

代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    tools:context=".ButtonActivity">

    <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#0066ff"
        android:text="按钮1"
        android:textColor="@color/white" />
    <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="按钮2"
        android:layout_below="@id/btn_1"
        android:textColor="@color/white"
        android:background="@drawable/bg_btn2" //引入背景颜色样式
        android:layout_marginTop="15dp" />
    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="按钮3"
        android:layout_below="@id/btn_2"
        android:textColor="#cc9900"
        android:background="@drawable/bg_btn3" //引入背景颜色样式
        android:layout_marginTop="15dp" />
    //定义按压效果
    <Button
        android:id="@+id/btn_4"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="按钮4"
        android:layout_below="@id/btn_3"
        android:textColor="@color/black"
        android:background="@drawable/bg_btn4" //引入背景颜色样式 按压效果
        android:layout_marginTop="15dp"
        android:onClick="showToast"/> //点击后的事件
    <TextView
        android:id="@+id/text4"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@id/btn_4"
        android:text="文字4"
        android:background="#cc9900"
        android:layout_marginTop="15dp"
        android:gravity="center"/>
</RelativeLayout>

 

//bg_btn2.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 按钮背景样式-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
<!--  背景填充  -->
    <solid
        android:color="#cc9900"/>
<!--  圆角5dp  -->
    <corners
        android:radius="5dp"/>

</shape>
//bg_btn3.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 按钮背景样式-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

<!--  按钮边框线样式  -->
    <stroke
        android:color="#cc9900" android:width="1dp"/>
    <!--  圆角5dp  -->
    <corners
        android:radius="5dp"/>

</shape>

 

设置button的background无效的问题,下面这行代码设置后按钮背景颜色没变还是原来的蓝紫色

<Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#0066ff"
        android:text="按钮1"
        android:textColor="@color/white" />

解决办法

找到项目中的这两个xml文件 将  parent="Theme.MaterialComponents.DayNight.DarkActionBar"  修改为  parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge" 即可解决

点击事件

public class ButtonActivity extends AppCompatActivity {

    private Button btn3;
    private TextView text4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);
        btn3 = findViewById(R.id.btn_3);
        text4 = findViewById(R.id.text4);
        btn3.setOnClickListener(v -> {
            Toast.makeText(ButtonActivity.this,"按钮3被点击了",Toast.LENGTH_SHORT).show();
        });
        text4.setOnClickListener(v -> {
            Toast.makeText(ButtonActivity.this,"文字4被点击了",Toast.LENGTH_SHORT).show();
        });
    }

    public void showToast(View view) {
        Toast.makeText(this,"哈哈",Toast.LENGTH_SHORT).show();
    }
}

效果

 

  • 15
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值