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

Sharktech:鲨鱼机房1Gbps无限流量美国服务器;丹佛$49/月起,洛杉矶$59/月起

sharktech怎么样?sharktech鲨鱼机房(Sharktech)我们也叫它SK机房,是一家成立于2003年的老牌国外主机商,提供的产品包括独立服务器租用、VPS主机等,自营机房在美国洛杉矶、丹佛、芝加哥和荷兰阿姆斯特丹等,主打高防产品,独立服务器免费提供60Gbps/48Mpps攻击防御。机房提供1-10Gbps带宽不限流量服务器,最低丹佛/荷兰机房每月49美元起,洛杉矶机房最低59美元...

CloudCone闪购优惠洛杉矶MC机房VPS月$1.99 便宜可随意删除重开

CloudCone商家我们很多喜欢低价便宜VPS主机的肯定是熟悉的,个人不是特别喜欢他。因为我之前测试过几次,开通的机器IP都是不通的,需要删除且开通好几次才能得到一个可用的IP地址。当然他们家的优势也是有的,就是价格确实便宜,而且还支持删除重新开通,而且机房只有一个洛杉矶MC。实话,如果他们家能多几个机房,保持现在的特点,还是有很多市场的。CloudCone是来自美国的主机销售商,成立于2017...

Fiberia.io:$2.9/月KVM-4GB/50GB/2TB/荷兰机房

Fiberia.io是个新站,跟ViridWeb.com同一家公司的,主要提供基于KVM架构的VPS主机,数据中心在荷兰Dronten。商家的主机价格不算贵,比如4GB内存套餐每月2.9美元起,采用SSD硬盘,1Gbps网络端口,提供IPv4+IPv6,支持PayPal付款,有7天退款承诺,感兴趣的可以试一试,年付有优惠但建议月付为宜。下面列出几款主机配置信息。CPU:1core内存:4GB硬盘:...

declare styleable为你推荐
豆瓣fm电台豆瓣怎么听音乐系统登录界面电脑启动总是出现登录界面怎么解决色温图色温,色调等参数怎么改?(图),还有什么建议吗?数据管理制度网络管理制度.碰撞球碰撞分为哪几种,分别解释一下vrrp配置在ospf中配置vrrp!那么vrrp需要宣告吗?团购网源码谁有功能比较全的团购网的代码?微信收费微信提现收费是怎么计算的 从什么时候开始收费比特币官方客户端如何查询比特币、莱特币确认 ?blacken“人非圣贤孰能无过”用英语怎么说
云南服务器租用 广州主机租用 simcentric 国外bt 42u机柜尺寸 万网优惠券 免费mysql 165邮箱 空间合租 傲盾官网 免费网页空间 linux使用教程 服务器防火墙 金主 登陆qq空间 带宽测试 accountsuspended vpsaa ddos攻击小组 主机箱 更多