完整项目示例Git仓库
https://git.oschina.net/jokerlee/CustomFrameLayout.git
Android View的宽高属性
View拥有的默认属性中含有minHeight以及minWidth可以控制view在其父View布局计算宽高时,能够有一个最小的宽高的限定;在进行一些布局的时候能够利用该属性来限定最小的宽高,但能否自己定义maxHeight和maxWidth来实现同样的限定view的宽高上限?
<Button android:id="@+id/button"
android:minHeight="155dp"
android:minWidth="240dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"/>
Android自定义attr属性
View除了android赋予的默认属性可以使用外,还提供了自己定义属性的方法,即在在res/values文件下新建一个attrs.xml,加入自定义的属性:
在布局xml使用自定义属性
在attrs.xml文件声明定义好自定义属性的名称和类型之后,在布局文件内加入:
xmlns:custom_attr="http://schemas.android.com/apk/res-auto"
xmlns:custom_attr="http://schemas.android.com/apk/res/[your package name]
新的官方文档建议使用第一个namespace声明(Android Studio仅支持该声明),旧版可使用第二种写法。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom_attr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context<