BackGround内外两种,Selector简用,App底部按钮=Selector背景选择器

在android中,给相应的布局设置背景图片有两种方法


第一,在布局中
即在相应布局中使用以下属性
android:background="@drawable/bg"   ///在资源文件中添加自己想要的背景图片 在此引用即可
 
第二,在代码中实现,如下
LinearLayout layout = (LinearLayout)this.findViewById(R.id.mylayout);
layout.setBackgroundResource(R.drawable.bg);

实现App界面底部功能栏按钮 

 

//首先先上一个效果图(选中变色):                图片:           

先准备两张图片然后在drawable下创建selector,创建item设置选中状态和非选中状态;

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/start_true" android:state_checked="true"></item>
    <item android:drawable="@drawable/start_false" android:state_checked="false"></item>

</selector>

   selector中各种状态选择,选择最适合你的那个 :

 

接下来式layout布局写法:

创建RadioGroup控件-->
子控件RadioButton:加权重,图片位于top方,调用写好的Selector,按钮设置为@null,最后gravity:center

 <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/btu_seletor"
            android:gravity="center"
            android:text="首页" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/btu_seletor"
            android:gravity="center"
            android:text="视频" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/btu_seletor"
            android:gravity="center"
            android:text="服务" />

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/btu_seletor"
            android:gravity="center"
            android:text="我的" />
    </RadioGroup>

 

 

在Android应用中,设置按钮(Button)的点击背景颜色通常涉及到XML布局文件和Java或Kotlin代码的配合。以下是基本步骤: 1. **在XML布局文件中**: - 创建一个`<Button>`元素,并给它一个ID,例如`android:id="@+id/my_button"`。 ```xml <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击我" /> ``` 2. **添加状态背景资源**: 使用`<selector>`标签定义按钮的不同状态下的背景颜色,包括默认、选中、按下等状态。 ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:background="#FF007ACC" /> <!-- 按下时的颜色 --> <item android:state_focused="true" android:background="#B3E5FC" /> <!-- 获得焦点时的颜色 --> <item android:state_selected="true" android:background="#B3E5FC" /> <!-- 选择时的颜色 --> <item android:background="#FFFFFF" /> <!-- 默认和未激活时的颜色 --> </selector> ``` 3. **Java/Kotlin代码关联颜色到按钮**: 在对应的Activity或Fragment中,找到按钮并设置它的背景颜色引用刚才创建的状态选择器。 ```java Button myButton = findViewById(R.id.my_button); myButton.setBackgroundDrawable(context.getDrawable(R.drawable.button_state_selector)); ``` 或者使用`StateListDrawable`: ```java StateListDrawable states = new StateListDrawable(); states.addState(new int[]{android.R.attr.state_pressed}, yourPressedColorDrawable); // 按下时的颜色 ... (其他状态) myButton.setBackground(states); ``` 4. **注意**: 如果你想动态改变按钮的点击背景颜色,可以监听`OnClickListener`并在回调中更新颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值