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

云基Yunbase无视CC攻击(最高500G DDoS防御),美国洛杉矶CN2-GIA高防独立服务器,

云基yunbase怎么样?云基成立于2020年,目前主要提供高防海内外独立服务器,欢迎各类追求稳定和高防优质线路的用户。业务可选:洛杉矶CN2-GIA+高防(默认500G高防)、洛杉矶CN2-GIA(默认带50Gbps防御)、香港CN2-GIA高防(双向CN2GIA专线,突发带宽支持,15G-20G DDoS防御,无视CC)。目前,美国洛杉矶CN2-GIA高防独立服务器,8核16G,最高500G ...

提速啦:美国多IP站群云服务器 8核8G 10M带宽 7IP 88元/月

提速啦(www.tisula.com)是赣州王成璟网络科技有限公司旗下云服务器品牌,目前拥有在籍员工40人左右,社保在籍员工30人+,是正规的国内拥有IDC ICP ISP CDN 云牌照资质商家,2018-2021年连续4年获得CTG机房顶级金牌代理商荣誉 2021年赣州市于都县创业大赛三等奖,2020年于都电子商务示范企业,2021年于都县电子商务融合推广大使。资源优势介绍:Ceranetwo...

个人网站备案流程及注意事项(内容方向和适用主机商)

如今我们还有在做个人网站吗?随着自媒体和短视频的发展和兴起,包括我们很多WEB2.0产品的延续,当然也包括个人建站市场的低迷和用户关注的不同,有些个人已经不在做网站。但是,由于我们有些朋友出于网站的爱好或者说是有些项目还是基于PC端网站的,还是有网友抱有信心的,比如我们看到有一些老牌个人网站依旧在运行,且还有新网站的出现。今天在这篇文章中谈谈有网友问关于个人网站备案的问题。这个也是前几天有他在选择...

declare styleable为你推荐
ie9下载IE9 beta版下载 IE9 beta版官方下载app退款app退款怎样才算恶意退款?aftereffectAfter effect Premere分别是什么软件,做什么的?豆瓣fm电台豆瓣电台怎么听自己喜欢歌手的歌豆瓣fm电台豆瓣和蜻蜓fm豆瓣fm电台豆瓣怎么听音乐高质量图片怎么高品质地保存图片色温图色温,色调等参数怎么改?(图),还有什么建议吗?阶乘函数用函数求阶乘的C语言阶乘函数C语言中有计算阶乘的函数吗 不是自己写,,,是那种可以直接调用的函数
老域名 美国主机排名 vir softbank官网 gitcafe evssl证书 北京主机 免费smtp服务器 河南服务器 警告本网站美国保护 腾讯云分析 100m空间 网络空间租赁 空间合租 如何安装服务器系统 Updog 安徽双线服务器 中国域名 腾讯数据库 hdroad 更多