- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章android 自定义控件 自定义属性详细介绍由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
自定义控件在android中无处不见,自定义控件给了我们很大的方便。比如说,一个视图为imageview ,imagebutton ,textview 等诸多控件的组合,用的地方有很多,我们不可能每次都来写3个的组合,既浪费时间,效率又低。在这种情况下,我们就可以自定义一个view来替换他们,不仅提升了效率并且在xml中运用也是相当的美观。 1、控件自定义属性介绍 以下示例中代码均在values/attrs.xml 中定义,属性均可随意命名。 1. reference:参考某一资源ID。 示例: [java] 。
复制代码 代码如下
<declare-styleable name = "名称"> <attr name = "background" format = "reference" /> <attr name = "src" format = "reference" /> </declare-styleable> 。
2. color:颜色值。 示例: [java] 。
复制代码 代码如下
<declare-styleable name = "名称"> <attr name = "textColor" format = "color" /> </declare-styleable> 。
3. boolean:布尔值。 示例: [java] 。
复制代码 代码如下
<declare-styleable name = "名称"> <attr name = "focusable" format = "boolean" /> </declare-styleable> 。
4. dimension:尺寸值。 示例: [java] 。
复制代码 代码如下
<declare-styleable name = "名称"> <attr name = "layout_width" format = "dimension" /> </declare-styleable> 。
5. float:浮点值。 示例: [java] 。
复制代码 代码如下
<declare-styleable name = "名称"> <attr name = "fromAlpha" format = "float" /> <attr name = "toAlpha" format = "float" /> </declare-styleable> 。
6. integer:整型值。 示例: [java] 。
复制代码 代码如下
<declare-styleable name = "名称"> <attr name = "frameDuration" format="integer" /> <attr name = "framesCount" format="integer" /> </declare-styleable> 。
7. string:字符串。 示例: [java] 。
复制代码 代码如下
<declare-styleable name = "名称"> <attr name = "text" format = "string" /> </declare-styleable> 。
8. fraction:百分数。 示例: [java] 。
复制代码 代码如下
<declare-styleable name="名称"> <attr name = "pivotX" format = "fraction" /> <attr name = "pivotY" format = "fraction" /> </declare-styleable> 。
9. enum:枚举值。 示例: [java] 。
复制代码 代码如下
<declare-styleable name="名称"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable> 。
10. flag:位或运算。 示例: [java] 。
复制代码 代码如下
<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.多类型。 示例: [java] 。
复制代码 代码如下
<declare-styleable name = "名称"> <attr name = "background" format = "reference|color" /> </declare-styleable> 。
------------------------------------------------------------------------------------------- 2、属性的使用以及自定义控件的实现 1、构思控件的组成元素,思考所需自定义的属性。 比如:我要做一个 <带阴影的按钮,按钮正下方有文字说明>(类似9宫格按钮) 新建values/attrs.xml [java] 。
复制代码 代码如下
<?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 :
复制代码 代码如下
package com.nanlus.custom; import com.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) { customListener.onCuscomClick(v, mCustomId); } } public interface CustomListener { void onCuscomClick(View v, int custom_id); } } 。
代码很简单,就不多说,下面来看看我们的CustomView是怎么用的,请看: 3、自定义控件的使用 话不多说,请看代码,main.xml: [java] 。
复制代码 代码如下
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:nanlus="http://schemas.android.com/apk/res/com.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" > <com.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" > </com.nanlus.custom.CustomView> </LinearLayout> </RelativeLayout> 。
在这里需要解释一下, xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom" nanlus为在xml中的前缀,com.nanlus.custom为包名 4、在Activity中,直接上代码 [java] 。
复制代码 代码如下
package com.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; import com.nanlus.BaseActivity; import com.nanlus.custom.R; import com.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 onCuscomClick(View v, int custom_id) { switch (custom_id) { case 1: Toast.makeText(this, "hello !!!", Toast.LENGTH_LONG).show(); break; default: break; } } } 。
最后此篇关于android 自定义控件 自定义属性详细介绍的文章就讲到这里了,如果你想了解更多关于android 自定义控件 自定义属性详细介绍的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
好的,所以我编辑了以下... 只需将以下内容放入我的 custom.css #rt-utility .rt-block {CODE HERE} 但是当我尝试改变... 与 #rt-sideslid
在表格 View 中,我有一个自定义单元格(在界面生成器中高度为 500)。在该单元格中,我有一个 Collection View ,我按 (10,10,10,10) 固定到边缘。但是在 tablev
对于我的无能,我很抱歉,但总的来说,我对 Cocoa、Swift 和面向对象编程还很陌生。我的主要来源是《Cocoa Programming for OS X》(第 5 版),以及 Apple 的充满
我正在使用 meta-tegra 为我的 NVIDIA Jetson Nano 构建自定义图像。我需要 PyTorch,但没有它的配方。我在设备上构建了 PyTorch,并将其打包到设备上的轮子中。现
在 jquery 中使用 $.POST 和 $.GET 时,有没有办法将自定义变量添加到 URL 并发送它们?我尝试了以下方法: $.ajax({type:"POST", url:"file.php?
Traefik 已经默认实现了很多中间件,可以满足大部分我们日常的需求,但是在实际工作中,用户仍然还是有自定义中间件的需求,为解决这个问题,官方推出了一个 Traefik Pilot[1] 的功
我想让我的 CustomTextInputLayout 将 Widget.MaterialComponents.TextInputLayout.OutlinedBox 作为默认样式,无需在 XML 中
我在 ~/.emacs 中有以下自定义函数: (defun xi-rgrep (term) (grep-compute-defaults) (interactive "sSearch Te
我有下表: 考虑到每个月的权重,我的目标是在 5 个月内分散 10,000 个单位。与 10,000 相邻的行是我最好的尝试(我在这上面花了几个小时)。黄色是我所追求的。 我试图用来计算的逻辑如下:计
我的表单中有一个字段,它是文件类型。当用户点击保存图标时,我想自然地将文件上传到服务器并将文件名保存在数据库中。我尝试通过回显文件名来测试它,但它似乎不起作用。另外,如何将文件名添加到数据库中?是在模
我有一个 python 脚本来发送电子邮件,它工作得很好,但问题是当我检查我的电子邮件收件箱时。 我希望该用户名是自定义用户名,而不是整个电子邮件地址。 最佳答案 发件人地址应该使用的格式是: You
我想减小 ggcorrplot 中标记的大小,并减少文本和绘图之间的空间。 library(ggcorrplot) data(mtcars) corr <- round(cor(mtcars), 1)
GTK+ noob 问题在这里: 是否可以自定义 GtkFileChooserButton 或 GtkFileChooserDialog 以删除“位置”部分(左侧)和顶部的“位置”输入框? 我实际上要
我正在尝试在主页上使用 ajax 在 magento 中使用 ajax 显示流行的产品列表,我可以为 5 或“N”个产品执行此操作,但我想要的是将分页工具栏与结果集一起添加. 这是我添加的以显示流行产
我正在尝试使用 PasswordResetForm 内置函数。 由于我想要自定义表单字段,因此我编写了自己的表单: class FpasswordForm(PasswordResetForm):
据我了解,新的 Angular 7 提供了拖放功能。我搜索了有关 DnD 的 Tree 组件,但没有找到与树相关的内容。 我在 Stackblitz 上找到的一个工作示例.对比drag'ndrop功能
我必须开发一个自定义选项卡控件并决定使用 WPF/XAML 创建它,因为我无论如何都打算学习它。完成后应该是这样的: 到目前为止,我取得了很好的进展,但还有两个问题: 只有第一个/最后一个标签项应该有
我要定制xtable用于导出到 LaTeX。我知道有些问题是关于 xtable在这里,但我找不到我要找的具体东西。 以下是我的表的外观示例: my.table <- data.frame(Specif
用ejs在这里显示日期 它给我结果 Tue Feb 02 2016 16:02:24 GMT+0530 (IST) 但是我需要表现为 19th January, 2016 如何在ejs中执行此操作?
我想问在 JavaFX 中使用自定义对象制作 ListView 的最佳方法,我想要一个每个项目如下所示的列表: 我搜了一下,发现大部分人都是用细胞工厂的方法来做的。有没有其他办法?例如使用客户 fxm
我是一名优秀的程序员,十分优秀!