zoukankan      html  css  js  c++  java
  • 10.Android开发笔记:布局控件(五) 自定义控件

    1.引入布局

    第一步:创建布局文件 activity_back.xml
    第二布:已入布局:
    java <include layout="@layout/activity_back"/>

    2.自定义 控件

    创建 布局文件 title.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/ic_launcher_background">
    
      <Button android:id="@+id/t_btn_goBack"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:text="返回" />
    
      <TextView android:id="@+id/t_btn_title"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:gravity="center"
          android:layout_gravity="center"
          android:background="#bbb"
          android:text="标题" />
    
      <Button android:id="@+id/bt_btn_detail"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:text="设置"/>
    </LinearLayout>
    

    创建java类:TitleLayout

    
    public class TitleLayout extends LinearLayout {
    
        public TitleLayout(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
    
            LayoutInflater.from(context).inflate(R.layout.title,this);//1
    
            Button btn_goBack = findViewById(R.id.t_btn_goBack);
            Button btn_detail = findViewById(R.id.bt_btn_detail);
            btn_goBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((Activity)getContext()).finish();//2
                }
            });
    
            btn_detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getContext(),"详情",Toast.LENGTH_LONG).show();
                }
            });
        }
    }
    
    

    使用自定义控

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
      <!--  <include layout="@layout/activity_back"/>-->
       <!-- <include layout="@layout/title"/>-->
    
      <com.example.customercontroller.myctrl.TitleLayout
          android:layout_width="match_parent"
          android:layout_height="53dp" />
    
    </LinearLayout>
    
    
  • 相关阅读:
    Spring Boot实现发送邮件
    IDEA thymeleaf ${xxx.xxx}表达式报错,红色波浪线
    解决springboot——集成 mybatis遇到的问题:No MyBatis mapper was found in '[com.example.demo]' package...
    解决Intellij IDEA中Mybatis Mapper自动注入警告
    System.gc()和Runtime.gc()的区别
    Java中定时器相关实现的介绍与对比之:Timer和TimerTask
    markdown语法介绍
    Java VisualVM使用
    Linux系统负载查询
    Kafka高性能吞吐关键技术分析
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/12463356.html
Copyright © 2011-2022 走看看