封装一些组合控件

本文介绍了一种自定义控件的实现方法,通过将常用的控件如底部导航的刷新和返回按钮组合并封装为一个大的控件,提高了代码复用性和开发效率。文中详细展示了如何创建自定义控件BackControl,并提供了具体的实现代码及使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对一些常用的几个控件进行组合,封装成一个大的控件。
比如底部导航上的刷新和返回按钮,多处用到,此时则封装成大的控件,事件处理可以在大控件内,也可以在外面。
说白了:布局。只是布局出现的地方不一样,一个是代码中,一个是xml中。
废话少说,代码:

public class BackControl extends LinearLayout{
 private Context context;
 private Button backBtn;
 public Button flashBtn;
 private View view;
 
 public BackControl(Context context) {
  super(context);
  this.context = context;
 }

 public BackControl(Context context, AttributeSet attrs) {
  super(context, attrs);
  this.context = context;
 }
 
 /**
     * 绑定布局,
     */
    public void bindLinearLayout(final int subModel) {
     LayoutInflater inflater = LayoutInflater.from(context);
     view = inflater.inflate(R.layout.user_bottom, null);
  addView(view, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  backBtn = (Button) view.findViewById(R.id.user_back);
  flashBtn = (Button) view.findViewById(R.id.user_flash);
  
  //返回按钮
  backBtn.setOnClickListener(new Button.OnClickListener() {
   @Override
   public void onClick(View v) {
    Tool.forwardTarget((Activity)context, subModel);
   }
  });
    }

如何使用:
back = (BackControl) findViewById(R.id.bulletlist_back);
  back.bindLinearLayout(DesktopUI.KJ);

 

在xml中:

<com.acp.control.BackControl
  android:layout_width="fill_parent" android:layout_weight="0"
  android:layout_height="wrap_content" android:id="@+id/userbetting_back">
 </com.acp.control.BackControl>
</LinearLayout>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值