declare styleableandroid自定义控件怎么用

declare styleable  时间:2022-02-27  阅读:()

android cardview 怎么用

CardView是安卓5.0的新控件,这控件其实就是一个卡片,当然我们自己也完全可以定义这样一个卡片,从现在的App中可以看到各式各样的自定义卡片,所以这个控件意义不是很大。support中的view所以使用在布局里面的时候一下子看不到效果的,比较不好。CardView继承的是FrameLayout,所以摆放内部控件的时候需要注意一下。 用代码来进行参数说明: <resources> <declare-styleable name="CardView"> <!-- Background color for CardView. --> <!-- 背景色 --> <attr name="cardBackgroundColor" format="color" /> <!-- Corner radius for CardView. --> <!-- 边缘弧度数 --> <attr name="cardCornerRadius" format="dimension" /> <!-- Elevation for CardView. --> <!-- 高度 --> <attr name="cardElevation" format="dimension" /> <!-- Maximum Elevation for CardView. --> <!-- 最大高度 --> <attr name="cardMaxElevation" format="dimension" /> <!-- Add padding in API v21+ as well to have the same measurements with previous versions. --> <!-- 设置内边距,v21+的版本和之前的版本仍旧具有一样的计算方式 --> <attr name="cardUseCompatPadding" format="boolean" /> <!-- Add padding to CardView on v20 and before to prevent intersections between the Card content and rounded corners. --> <!-- 在v20和之前的版本中添加内边距,这个属性是为了防止卡片内容和边角的重叠 --> <attr name="cardPreventCornerOverlap" format="boolean" /> <!-- 下面是卡片边界距离内部的距离--> <!-- Inner padding between the edges of the Card and children of the CardView. --> <attr name="contentPadding" format="dimension" /> <!-- Inner padding between the left edge of the Card and children of the CardView. --> <attr name="contentPaddingLeft" format="dimension" /> <!-- Inner padding between the right edge of the Card and children of the CardView. --> <attr name="contentPaddingRight" format="dimension" /> <!-- Inner padding between edge of the Card and children of the CardView. --> <attr name="contentPaddingTop" format="dimension" /> <!-- Inner padding between the bottom edge of the Card and children of the CardView. --> <attr name="contentPaddingBottom" format="dimension" /> </declare-styleable> </resources> 看完了参数,那么我们来看看布局文件中的用法。 <!-- A CardView that contains a TextView --> <android.support.v7.widget.CardView xmlns:android="/apk/res/android" xmlns:tools="/tools" xmlns:app="/apk/res-auto" android:id="@+id/card_view" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" app:cardCornerRadius="4dp" app:cardBackgroundColor="#ff0000" app:cardElevation="5dp" app:cardMaxElevation="10dp" app:cardUseCompatPadding="true" app:cardPreventCornerOverlap="true"> <TextView android:id="@+id/info_text" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:textSize="25sp" android:textColor="#ffffff" android:text="Hello CardView"/> </android.support.v7.widget.CardView> 以上就是详细使用说明。 这样我们就定义好了一个CardView了。

android自定义控件怎么用

一、控件自定义属性介绍 以下示例中代码均在values/attrs.xml 中定义,属性均可随意命名。 1. reference:参考某一资源ID。 示例: <declare-styleable name = "名称"> <attr name = "background" format = "reference" /> <attr name = "src" format = "reference" /> </declare-styleable> 2. color:颜色值。 示例: <declare-styleable name = "名称"> <attr name = "textColor" format = "color" /> </declare-styleable> 3. boolean:布尔值。 示例: <declare-styleable name = "名称"> <attr name = "focusable" format = "boolean" /> </declare-styleable> 4. dimension:尺寸值。 示例: <declare-styleable name = "名称"> <attr name = "layout_width" format = "dimension" /> </declare-styleable> 5. float:浮点值。 示例: <declare-styleable name = "名称"> <attr name = "fromAlpha" format = "float" /> <attr name = "toAlpha" format = "float" /> </declare-styleable> 6. integer:整型值。 示例: <declare-styleable name = "名称"> <attr name = "frameDuration" format="integer" /> <attr name = "framesCount" format="integer" /> </declare-styleable> 7. string:字符串。 示例: <declare-styleable name = "名称"> <attr name = "text" format = "string" /> </declare-styleable> 8. fraction:百分数。 示例: <declare-styleable name="名称"> <attr name = "pivotX" format = "fraction" /> <attr name = "pivotY" format = "fraction" /> </declare-styleable> 9. enum:枚举值。 示例: <declare-styleable name="名称"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable> 10. flag:位或运算。 示例: <declare-styleable name="名称"> <attr name="windowSoftInputMode"> <flag name = "stateUnspecified" value = "0" /> <flag name = "stateUnchanged" value = "1" /> <flag name = "stateHidden" value = "2" /> <flag name = "stateAlwaysHidden" value = "3" /> </attr> </declare-styleable> 11.多类型。 示例: <declare-styleable name = "名称"> <attr name = "background" format = "reference|color" /> </declare-styleable> 二、属性的使用以及自定义控件的实现 1、构思控件的组成元素,思考所需自定义的属性。 比如:我要做一个 <带阴影的按钮,按钮正下方有文字说明>(类似9宫格按钮) 新建values/attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="custom_view"> <attr name="custom_id" format="integer" /> <attr name="src" format="reference" /> <attr name="background" format="reference" /> <attr name="text" format="string" /> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable> </resources> 以上,所定义为custom_view,custom_id为按钮id,src为按钮,background为阴影背景,text为按钮说明,textColor为字体颜色,textSize为字体大小。 2、怎么自定义控件呢,怎么使用这些属性呢?话不多说请看代码,CustomView : .nanlus.custom; .nanlus.custom.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; public class CustomView extends FrameLayout implements OnClickListener { private CustomListener customListener = null; private Drawable mSrc = null, mBackground = null; private String mText = ""; private int mTextColor = 0; private float mTextSize = 20; private int mCustomId = 0; private ImageView mBackgroundView = null; private ImageButton mButtonView = null; private TextView mTextView = null; private LayoutParams mParams = null; public CustomView(Context context) { super(context); } public CustomView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.custom_view); mSrc = a.getDrawable(R.styleable.custom_view_src); mBackground = a.getDrawable(R.styleable.custom_view_background); mText = a.getString(R.styleable.custom_view_text); mTextColor = a.getColor(R.styleable.custom_view_textColor, Color.WHITE); mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20); mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0); mTextView = new TextView(context); mTextView.setTextSize(mTextSize); mTextView.setTextColor(mTextColor); mTextView.setText(mText); mTextView.setGravity(Gravity.CENTER); mTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mButtonView = new ImageButton(context); mButtonView.setImageDrawable(mSrc); mButtonView.setBackgroundDrawable(null); mButtonView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mButtonView.setOnClickListener(this); mBackgroundView = new ImageView(context); mBackgroundView.setImageDrawable(mBackground); mBackgroundView.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); addView(mBackgroundView); addView(mButtonView); addView(mTextView); this.setOnClickListener(this); a.recycle(); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); mParams = (LayoutParams) mButtonView.getLayoutParams(); if (mParams != null) { mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP; mButtonView.setLayoutParams(mParams); } mParams = (LayoutParams) mBackgroundView.getLayoutParams(); if (mParams != null) { mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP; mBackgroundView.setLayoutParams(mParams); } mParams = (LayoutParams) mTextView.getLayoutParams(); if (mParams != null) { mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; mTextView.setLayoutParams(mParams); } } public void setCustomListener(CustomListener l) { customListener = l; } @Override public void onClick(View v) { if (customListener != null) { Click(v, mCustomId); } } public interface CustomListener { void Click(View v, int custom_id); } } 代码很简单,就不多说,下面来看看我们的CustomView是怎么用的,请看: 3、自定义控件的使用 话不多说,请看代码,main.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="/apk/res/android" xmlns:nanlus="/apk/.nanlus.custom" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:orientation="horizontal" > &.nanlus.custom.CustomView android:id="@+id/custom1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" nanlus:background="@drawable/background" nanlus:custom_id="1" nanlus:src="@drawable/style_button" nanlus:text="按钮1" > <.nanlus.custom.CustomView> </LinearLayout> </RelativeLayout> 在这里需要解释一下, xmlns:nanlus="/apk/.nanlus.custom" nanlus为在xml中的前缀.nanlus.custom为包名 4、在Activity中,直接上代码 .nanlus.custom; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; .nanlus.BaseActivity; .nanlus.custom.R; .nanlus.custom.CustomView.CustomListener; public class CustomActivity extends BaseActivity implements CustomListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this); } @Override public void Click(View v, int custom_id) { switch (custom_id) { case 1: Toast.makeText(this, "hello !!!", Toast.LENGTH_LONG).show(); break; default: break; } } }

ThomasHost(月付5美元)美国/法国/英国/加拿大KVM,支持Windows

ThomasHost域名注册自2012年,部落最早分享始于2016年,还算成立了有几年了,商家提供基于KVM架构的VPS,数据中心包括美国、法国、英国、加拿大和爱尔兰等6个地区机房,VPS主机套餐最低2GB内存起步,支持Windows或者Linux操作系统,1Gbps端口不限制流量。最近商家提供了一个5折优惠码,优惠后最低套餐月付5美元起。下面列出部分套餐配置信息。CPU:1core内存:2GB硬...

快快云:香港沙田CN2/美国Cera大宽带/日本CN2,三网直连CN2 GIA云服务器和独立服务器

快快云怎么样?快快云是一家成立于2021年的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务,快快云目前提供有香港云服务器、美国云服务器、日本云服务器、香港独立服务器、美国独立服务器,日本独立服务器。快快云专注为个人开发者用户,中小型,大型企业用户提供一站式核心网络云端服务部署,促使用户云端部署化简为零,轻松快捷运用云计算!多年云计算领域服务经验,遍布亚太地区的海量节点为业务推进提供强大...

IMIDC(rainbow cloud):香港/台湾/日本/莫斯科独立服务器特价,闪购大促销,最低30usd/月起

imidc怎么样?imidc彩虹网路,rainbow cloud知名服务器提供商。自营多地区数据中心,是 Apnic RIPE Afrinic Arin 认证服务商。拥有丰富的网路资源。 在2021年 6.18 开启了输血大促销,促销区域包括 香港 台湾 日本 莫斯科 等地促销机型为 E3係,参与促销地区有 香港 日本 台湾 莫斯科 等地, 限量50台,售罄为止,先到先得。所有服务器配置 CPU ...

declare styleable为你推荐
网页图片显示不出来电脑的部分网页图片显示不出来是怎么回事?oracle11g下载我从oracle官网上下载了 oracle11g 不知道怎么安装ripperRipper是个什么病毒巴西时区巴西和中国的时差是多少 里约和北京时差怎么算安卓模拟器哪个好用电脑上的手机模拟器有哪些?哪个更好一点?qsv视频格式转换器手机qsv怎么转换成mp4格式转换器vrrp配置我准备做一个关于MSTP的配置,但是不知道如何去做,拓扑如下手机壳生产厂家寻找制作手机壳的厂家有哪些?廖华100个成语典故及其历史人物故事 南京廖华网页错误详细信息网页错误详细信息 消息: 'this._self.style' 为空或不是对象
域名主机 过期域名 云南服务器租用 阿里云os openv 狗爹 parseerror 建站代码 主机合租 工作站服务器 佛山高防服务器 php空间购买 免费dns解析 七夕快乐英语 网页提速 英雄联盟台服官网 成都主机托管 免备案cdn加速 汤博乐 ncp 更多