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; } } }

virmach:AMD平台小鸡,赌一把,单车变摩托?$7.2/年-512M内存/1核/10gSSD/1T流量,多机房可选

virmach送来了夏季促销,价格低到爆炸,而且在低价的基础上还搞首年8折,也就是说VPS低至7.2美元/年。不过,这里有一点要说明:你所购买的当前的VPS将会在09/30/2021 ~ 04/30/2022进行服务器转移,而且IP还会改变,当前的Intel平台会换成AMD平台,机房也会变动(目前来看以后会从colocrossing切换到INAP和Psychz),采取的是就近原则,原来的水牛城可能...

创梦网络-江苏宿迁BGP云服务器100G高防资源,全程ceph集群存储,安全可靠,数据有保证,防护真实,现在购买7折促销,续费同价!

官方网站:点击访问创梦网络宿迁BGP高防活动方案:机房CPU内存硬盘带宽IP防护流量原价活动价开通方式宿迁BGP4vCPU4G40G+50G20Mbps1个100G不限流量299元/月 209.3元/月点击自助购买成都电信优化线路8vCPU8G40G+50G20Mbps1个100G不限流量399元/月 279.3元/月点击自助购买成都电信优化线路8vCPU16G40G+50G2...

RAKsmart新年钜惠:E3服务器秒杀$30/月起,新上韩国服务器,香港/日本/美国站群服务器,VPS月付$1.99起,GPU服务器,高防服务器_vps香港

RAKsmart发布了新年钜惠活动,即日起到2月28日,商家每天推出限量服务器秒杀,美国服务器每月30美元起,新上了韩国服务器、GPU服务器、香港/日本/美国常规+站群服务器、1-10Gbps不限流量大带宽服务器等大量库存;VPS主机全场提供7折优惠码,同时针对部分特惠套餐无码直购每月仅1.99美元,支持使用PayPal或者支付宝等方式付款,有中英文网页及客服支持。爆款秒杀10台/天可选精品网/大...

declare styleable为你推荐
注册表命令常用的注册表命令有那些?bft请问BFT高级是什么水平的?安全防护安全防护措施apple以旧换新苹果以旧换新吗活动 可以换钱吗,还是只能折抵新手机wizardry哈利波特里的蛇院,狮院,獾院,鹰院. 分别指什么高质量图片ps 合成图片,怎样才算高质量的?从那些方面判定照片的质量蓝牙开发开发者选项里的蓝牙设置如何设置最好?微店是什么微店和淘宝网店有啥区别?免杀远控求一款好使(免杀)远程控制软件?3d规则福利彩票3D的中奖规则
info域名注册 虚拟主机试用30天 主机测评 budgetvm 英文站群 老左来了 中国电信测网速 cdn加速原理 空间技术网 国外免费asp空间 优酷黄金会员账号共享 腾讯总部在哪 512mb 免费asp空间申请 免费蓝钻 工信部icp备案查询 免费php空间 购买空间 ssl加速 乐视会员免费领取 更多