android 使用selector 在 button上无效的问题(汇总)

本文介绍了在Android开发中关于UI资源管理的三个要点:1. 图片或按钮的XML文件应放置于特定的drawable-h文件夹内;2. selector文件中的默认状态项必须置于最后;3. 避免资源文件名重复,特别是selector与drawable文件名冲突的情况。



问题有三个:



1.图片或者按钮的XML文件,应该写在drawable-h的文件夹下面,不能写在drawable里面



2.selector这个文件,默认的ITEM加载项,必须写在最后:

EG:

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  
    <item android:drawable="@drawable/loginactivity_regist_login_press" android:state_focused="true"/> 
    <item android:drawable="@drawable/loginactivity_regist_login_press" android:state_selected="true"/> 
    <item android:drawable="@drawable/loginactivity_regist_login_press" android:state_pressed="true"/> 
    <item android:drawable="@drawable/loginactivity_regist_login_press" android:state_enabled="false"/> 
    <item android:drawable="@drawable/loginactivity_regist_login_normal"/>   <!-- 这行,放在最后面就成功了> 
</selector>



3.不能使用重复名字的文件,及时SELCTOR的文件名名称补能和DRAWABLE的名称一致。


其他的问题,请参照google的官方example的例子来做。


kotlin:package com.example.emptytapo.homepage import androidx.fragment.app.viewModels import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageButton import androidx.recyclerview.widget.GridLayoutManager import com.example.emptytapo.R import com.example.emptytapo.databinding.FragmentHomePageBinding class HomePageFragment : Fragment() { private var _binding: FragmentHomePageBinding? = null private val binding get() = _binding!! private val devices = mutableListOf( Device("Bulb", R.drawable.bulb), Device("Camera", R.drawable.camera), Device("Plug", R.drawable.plug), ) private val deviceList = ArrayList<Device>() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { _binding = FragmentHomePageBinding.inflate(inflater, container, false) return binding.root initDevice() val layoutManager = GridLayoutManager(requireContext(), 2) binding.cardRecycleContainer.layoutManager = layoutManager val adapter = DeviceAdapter(requireContext(), deviceList) binding.cardRecycleContainer.adapter = adapter return view } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val buttons = listOf(binding.btnHome, binding.btnSmart, binding.btnMe) buttons.forEach { btn -> btn.setOnClickListener { buttons.forEach { it.isSelected = false } btn.isSelected = true } } } private fun initDevice() { deviceList.clear() repeat(10) { val index = (0 until devices.size).random() deviceList.add(devices[index]) } } override fun onDestroyView() { super.onDestroyView() _binding = null } } xml: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:id=“@+id/main” android:layout_width=“match_parent” android:layout_height=“match_parent” android:background=“@color/white” tools:context=“.mainpage.MainPageActivity”> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/my_home_pic" android:layout_width="match_parent" android:layout_height="230dp" android:background="@drawable/myhomebackground"></ImageView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="80dp" android:text="My home" android:textColor="@color/black" android:textSize="27dp" android:textStyle="bold"></TextView> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayoutPartial" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="130dp"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="20dp"> <!--文字--> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rounded_background" android:text="Behind TV2 (Plug) will trun on at 7:00 PM"> </TextView> <!--文字--> <TextView android:id="@+id/camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/time" android:layout_marginTop="20dp" android:background="@drawable/rounded_background" android:text="2 Cameras"> </TextView> <!--卡片布局--> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/card_recycle_container" android:layout_width="match_parent" android:layout_height="match_parent"> </androidx.recyclerview.widget.RecyclerView> </androidx.coordinatorlayout.widget.CoordinatorLayout> </RelativeLayout> <!--导航栏--> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> <!--toolbar--> <androidx.appcompat.widget.Toolbar android:id="@+id/bottom_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@color/white" android:paddingTop="8dp" android:paddingBottom="8dp"> <!-- 使用 LinearLayout 作为容器放置 3 个 ImageButton --> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/button_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" android:weightSum="3"> <ImageButton android:id="@+id/btn_home" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="?attr/selectableItemBackgroundBorderless" android:src="@drawable/home_selector"></ImageButton> <ImageButton android:id="@+id/btn_smart" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="?attr/selectableItemBackgroundBorderless" android:contentDescription="搜索" android:src="@drawable/smart_selector" /> <ImageButton android:id="@+id/btn_me" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="?attr/selectableItemBackgroundBorderless" android:contentDescription="个人中心" android:src="@drawable/me_selector" /> </LinearLayout> </RelativeLayout> </androidx.appcompat.widget.Toolbar> </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout> Device:package com.example.emptytapo.homepage class Device(val name: String,val imageID: Int) { } DeviceAdapter package com.example.emptytapo.homepage import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.ViewHolder import com.bumptech.glide.Glide import com.example.emptytapo.R class DeviceAdapter(val context: Context, val deviceList: List<Device>) : RecyclerView.Adapter<DeviceAdapter.ViewHolder>() { inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val deviceImage: ImageView = view.findViewById(R.id.deviceImage) val deviceName: TextView = view.findViewById(R.id.deviceName) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(context).inflate(R.layout.device_item, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val device = deviceList[position] holder.deviceName.text = device.name Glide.with(context).load(device.imageID).into(holder.deviceImage) } override fun getItemCount()=deviceList.size } devilce_item.xml:<?xml version="1.0" encoding="utf-8"?> <com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" app:cardCornerRadius="4dp"> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/deviceName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:textSize="14sp" android:textStyle="bold" android:text="Tapo Plug"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:textSize="12sp" android:text="Plug 8"/> <ImageView android:id="@+id/deviceImage" android:layout_marginTop="20dp" android:background="@drawable/bulb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="centerCrop" /> </LinearLayout> </com.google.android.material.card.MaterialCardView>
最新发布
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值