UICustomViews 自定义布局和控件

自定义控件

引入布局

  • 创建title.xml

    • android:background="@drawable/back_bg" 背景图片
    • android:layout_margin="5dp" 控件上下左右偏移距离 也可以marginTop单独指定
    • <include layout="@layout/title"/> 在activity_main.xml 中 就可以显示title.xml 标题栏

      <android.support.constraint.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:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context="com.example.mason.uicustomviews.MainActivity">
      
          <include layout="@layout/title"/>
      
      </android.support.constraint.ConstraintLayout>
    • 设置系统自带标题栏隐藏

      1. 调用getSupportActionBar() 方法获得 ActionBar 实例
      2. 调用ActionBar的hide()方法 隐藏标题栏
        “`java
        public class MainActivity extends AppCompatActivity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      ActionBar actionBar = getSupportActionBar();
      if(actionBar!=null){
          actionBar.hide();
      }
      

      }
      }

创建自定义控件


  • 相同功能的控件
  • 新建TitleLayout 继承自LinearLayout
  • 重写LinearLayout中带有两个参数的构造函数
  • 布局引入TitleLayout 调用这个函数,
  • 通过LinearLayout的from()方法构建一个LayoutInflater对象
  • 调用inflate()方法加载布局文件
  • inflate()接收两个参数 要加载布局的id和加载好的布局再添加一个父布局
    • 新建TitleLayout.java
      java
      public class TitleLayout extends LinearLayout{
      public TitleLayout(Context context, AttributeSet attrs){
      super(context,attrs);
      LayoutInflater.from(context).inflate(R.layout.title,this);
      }
      }
    • xml 文件
      xml
      <com.example.mason.uicustomviews.TitleLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
    • TitleLayout.java设置点击事件 getContext()
      java
      public class TitleLayout extends LinearLayout {
      public TitleLayout(Context context, AttributeSet attrs) {
      super(context, attrs);
      LayoutInflater.from(context).inflate(R.layout.title, this);
      Button titleEdit = (Button) findViewById(R.id.title_edit);
      titleEdit.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      Toast.makeText(getContext(), "you click edit", Toast.LENGTH_SHORT).show();
      }
      });
      Button titleBack = (Button) findViewById(R.id.title_back);
      titleBack.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      ((Activity) getContext()).finish();
      }
      });
      }
      }

这是我学习Android的笔记,参考书<<第一行代码-第二版>>,感谢郭霖大神
如有不足之处,可以一起讨论
附上郭霖的blog http://blog.csdn.net/guolin_blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值