当前位置: 移动技术网 > 移动技术>移动开发>Android > Android连载1-自定义UI控件

Android连载1-自定义UI控件

2020年04月02日  | 移动技术网移动技术  | 我要评论

一、 对ui界面尽心自定义组件

package com.example.uicustomviews;

​

import android.app.activity;

import android.os.bundle;

import android.view.menu;

import android.view.menuitem;

import android.view.window;

​

public class mainactivity extends activity {

​

  @override

  protected void oncreate(bundle savedinstancestate) {

    super.oncreate(savedinstancestate);

    requestwindowfeature(window.feature_no_title);

    setcontentview(r.layout.activity_main);

  }

​

}

 

​

<?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:background="@drawable/title_bg"

    >

    <!-- 这里我们自定义了一个背景图片title_bg,并且在res文件夹中保存了这张图片 -->

    <!-- 下面的margin即代表外边距,类似于之前学过的html/css中的属性设置 -->

    <button

        android:id="@+id/title_back"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:layout_margin="5dip"

        android:background="@drawable/title_bg"

        android:text="back"

        android:textcolor="#fff"

        />

    <textview

        android:id="@+id/title_text"

        android:layout_width="0dip"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:layout_weight="1"

        android:gravity="center"

        android:text="title text"

        android:textcolor="#fff"

        android:textsize="24sp" />

   

    <button

        android:id="@+id/title_edit"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:layout_margin="5dip"

        android:background="@drawable/title_bg"

        android:text="edit"

        android:textcolor="#fff" />

​

</linearlayout>

 

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

  android:layout_width="match_parent"

  android:layout_height="match_parent" >

 

    <com.example.uicustomviews.titlelayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

       

    </com.example.uicustomviews.titlelayout>

​

    <include layout="@layout/title" />

​

  <!-- 这里使用include语句来导入我们刚才写的标题xml -->

​

</linearlayout>

 

package com.example.uicustomviews;

import android.widget.linearlayout;

import android.widget.toast;

import android.content.context;

import android.util.attributeset;

import android.view.layoutinflater;

import android.widget.button;

import android.widget.toast;

//import android.

​

​

public class titlelayout extends linearlayout{

  public titlelayout(context context,attributeset attrs) {

    super(context,attrs);

    layoutinflater.from(context).inflate(r.layout.title,this);

    button titleback = (button) findviewbyid(r.id.title_back);

    button titleedit = (button) findviewbyid(r.id.title_edit);

    titleback.setonclicklistener(new onclicklistener() {

      @override

      public void onclick(view v) {

        ((activity) getcontext()).finish();

      }

    });

    titleedit.setonclicklistener(new onclicklistenter() {

      @override

      public void onclick(view v) {

        toast.maketext(getcontext(),"you clicked edit button", toast.length_short).show();

      }

    });

  }

}

分别定义了活动与​界面。​

二、源码:

1.项目地址

https://github.com/ruigege66/android/tree/master/uicustomviews

2.csdn:https://blog.csdn.net/weixin_44630050

3.博客园:https://www.cnblogs.com/ruigege0000/

4.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网