对于想了解android–在XML中使用自定义视图而不使用完全限定的类名的读者,本文将是一篇不可错过的文章,我们将详细介绍androidxml中加入自定义view,并且为您提供关于Andriod:在x
对于想了解android – 在XML中使用自定义视图而不使用完全限定的类名的读者,本文将是一篇不可错过的文章,我们将详细介绍android xml中加入自定义view,并且为您提供关于Andriod: 在xml布局中使用自定义属性、android ProgressDialog:设置自定义视图而不是消息 – 这有用吗?、Android xml布局并添加以xml编写的自定义视图、android – Linkify不使用自定义模式的有价值信息。
本文目录一览:- android – 在XML中使用自定义视图而不使用完全限定的类名(android xml中加入自定义view)
- Andriod: 在xml布局中使用自定义属性
- android ProgressDialog:设置自定义视图而不是消息 – 这有用吗?
- Android xml布局并添加以xml编写的自定义视图
- android – Linkify不使用自定义模式
android – 在XML中使用自定义视图而不使用完全限定的类名(android xml中加入自定义view)
我有自己的样式用于定义为主题的按钮,但我也使用自己的类来处理按钮(因为自己的字体).可以用一个漂亮的名字来调用我的按钮,例如
<MyButton>
代替
<com.wehavelongdomainname.android.ui.MyButton>
解决方法:
令人惊讶的是,答案是肯定的.我最近了解到这一点,实际上你可以采取一些措施来提高你的自定义视图通胀效率. IntelliJ仍警告你它无效(虽然它会编译并成功运行) – 我不确定Eclipse是否会警告你.
无论如何,你需要做的是定义自己的LayoutInflater.Factory子类:
public class CustomViewFactory implements LayoutInflater.Factory {
private static CustomViewFactory mInstance;
public static CustomViewFactory getInstance () {
if (mInstance == null) {
mInstance = new CustomViewFactory();
}
return mInstance;
}
private CustomViewFactory () {}
@Override
public View onCreateView (String name, Context context, AttributeSet attrs) {
//Check if it's one of our custom classes, if so, return one using
//the Context/AttributeSet constructor
if (MyCustomView.class.getSimpleName().equals(name)) {
return new MyCustomView(context, attrs);
}
//Not one of ours; let the system handle it
return null;
}
}
然后,在您向包含这些自定义视图的布局进行充气的任何活动或上下文中,您需要将工厂分配给该上下文的LayoutInflater:
public class CustomViewActivity extends Activity {
public void onCreate (Bundle savedInstanceState) {
//Get the LayoutInflater for this Activity context
//and set the Factory to be our custom view factory
LayoutInflater.from(this).setFactory(CustomViewFactory.getInstance());
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_with_custom_view);
}
}
然后,您可以在XML中使用简单的类名:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<MyCustomView
android:id="@+id/my_view"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="center_vertical" />
</FrameLayout>
Andriod: 在xml布局中使用自定义属性
转载:http://www.cnblogs.com/hibraincol/archive/2011/10/27/2227149.html
今天在看android froyo的launcher2 源码的时候,在launcher.xml中看到有这么一段代码:
<
com.android.launcher2.DragLayer
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:launcher
"http://schemas.android.com/apk/res/com.android.launcher"
android:id
"@+id/drag_layer"
android:layout_width
"match_parent"
android:layout_height
"match_parent"
>
include
layout
"@layout/all_apps"
/>
<!-- The workspace contains 3 screens of cells -->
com.android.launcher2.Workspace
"@+id/workspace"
"match_parent"
"match_parent"
android:scrollbars
"horizontal"
android:fadeScrollbars
"true"
launcher:defaultScreen
"2"
>
|
注意到其中的两处:
xmlns:launcher=”http://schemas.android.com/apk/res/com.android.launcher”
和
launcher:defaultScreen="2"
可以看出在这个布局文件中,使用了自定义属性。
以前没遇到过,既然这里碰到了,就顺便学习下,下面就写个简单的示例,权当学习笔记,便于以后查阅。
1. 定义一些自定义属性
建立一个属性xml文件: values/attrs.xml,内容如下:
xml
version
"1.0"
encoding
"utf-8"
?>
>
name
"relation"
>
"icon_left"
value
"0"
/>
"1"
/>
/>
"3"
/>
attr
>
/>
"IconText"
>
format
"reference"
/>
"string"
/>
"dimension"
/>
"integer"
/>
/>
>
解释如下:
属性relation有4种可选值:icon_left,icon_right,icon_above,icon_below.
属性icon的可选值为引用: 例如:"@/drawbable/icon".
属性text的可选值为string, 例如: "Hello world",也可是string的引用"@string/hello".
属性text_size的可选值为尺寸大小,例如:20sp、18dip、20px等.
属性text_color的可选值为整数,例如:"0xfffffff", 也可以是color的引用"@color/white".
2. 定义一个能够处理这些属性值的view或者layout类
package
com.braincol.viewattrs;
android.content.Context;
class
IconTextView
extends
LinearLayout {
private
final
static
String TAG =
"IconTextView"
;
ICON_LEFT =
0
;
1
;
2
;
3
;
TextView mTextView;
""
;
mTextSize;
IconTextView(Context context,AttributeSet attrs){
(context,attrs);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.IconText);
"mRelation: "
+mRelation);
+mText);
12
);
+mTextSize);
);
+mSpace);
+mIconId);
new
TextView(context);
ImageView(context);
;
;
;
;
;
(mRelation){
ICON_ABOVE:
;
.setorientation(orientation);
}
android ProgressDialog:设置自定义视图而不是消息 – 这有用吗?
在ProgressDialog的文档中,它说:
“显示进度指示器和可选文本消息或视图的对话框.只能同时使用文本消息或视图.”
我已经让它与一条消息很漂亮地工作,但我想使用自定义视图 – 带有取消按钮的消息.但是在ProgressDialog上调用setView()似乎没有任何效果 – 它在对话框中显示进度条但没有其他内容.我只用一个带有文本“hello”的TextView尝试过,但它没有显示出来.
有谁知道如何使这个功能工作?这是一个真正的功能吗?
解决方法:
可能的解决方法是使用ProgressBar创建自定义对话框.
Android xml布局并添加以xml编写的自定义视图
我创建了一个自定义视图XML布局资源MyView.xml.我可以在第二个XML布局(例如main.xml)中通过XML布局文件引用MyView吗?如果是这样,怎么办?
(要明确,我不是在寻找可以解释如何从我的Java代码中扩展视图的答复,也不是可以解释使用< com.MyDomain.App.MyView ...>在XML布局资源中引入java类的答复,除非这有助于这里或澄清我别无选择.)
解决方法:
如果您的XML文件类似于/res/layout/MyView.xml,则可以在main.xml中使用
<?xml版本=“ 1.0”编码=“ utf-8”?>
…
< include layout =“ @ layout / MyView.xml” />
…
android – Linkify不使用自定义模式
我写了以下代码 –
Pattern japPhonenopattern = Pattern.compile("[0-9]{10,11}"); Linkify.addLinks(viewHolder.right_message,japPhonenopattern,"");
但它并没有将文本转换为链接.
知道我可能做错了什么吗?
如果它很重要,我在ListView中使用linkify.
解决方法
<TextView android:id="@+id/test_phone_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="12345678910"/>
码
TextView t4 = (TextView)findViewById(R.id.test_phone_number); Linkify.addLinks(t4,Linkify.PHONE_NUMBERS);
关于android – 在XML中使用自定义视图而不使用完全限定的类名和android xml中加入自定义view的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Andriod: 在xml布局中使用自定义属性、android ProgressDialog:设置自定义视图而不是消息 – 这有用吗?、Android xml布局并添加以xml编写的自定义视图、android – Linkify不使用自定义模式的相关知识,请在本站寻找。
本文标签: