Ⅰ android 自定义view要重写哪几个方法
很多的Android入门程序猿来说对于Android自定义View,可能都是比较恐惧的,但是这又是高手进阶的必经之路,所有准备在自定义View上面花一些功夫,多写一些文章。先总结下自定义View的步骤: 1、自定义View的属性 2、在View的构造方法中获得我们自定义的属性 [ 3、重写onMesure ] 4、重写onDraw 我把3用[]标出了,所以说3不一定是必须的,当然了大部分情况下还是需要重写的。 详见网址:blog/lmj623565791/article/details/24252901
Ⅱ android 自定义view 怎么规定view的样式
android 自定义view的样式的实现:
1.在values文件夹下,打开attrs.xml,其实这个文件名称可以是任意的,写在这里更规范一点,表示里面放的全是view的属性。
2.因为我们下面的实例会用到2个长度,一个颜色值的属性,所以我们这里先创建3个属性。
<declare-styleable name="rainbowbar">
<attr name="rainbowbar_hspace" format="dimension"></attr>
<attr name="rainbowbar_vspace" format="dimension"></attr>
<attr name="rainbowbar_color" format="color"></attr>
</declare-styleable>
举例说明:
蓝色的进度条
public class RainbowBar extends View {
//progress bar color
int barColor = Color.parseColor("#1E88E5");
//every bar segment width
int hSpace = Utils.dpToPx(80, getResources());
//every bar segment height
int vSpace = Utils.dpToPx(4, getResources());
//space among bars
int space = Utils.dpToPx(10, getResources());
float startX = 0;
float delta = 10f;
Paint mPaint;
public RainbowBar(Context context) {
super(context);
}
public RainbowBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RainbowBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//read custom attrs
TypedArray t = context.obtainStyledAttributes(attrs,
R.styleable.rainbowbar, 0, 0);
hSpace = t.getDimensionPixelSize(R.styleable.rainbowbar_rainbowbar_hspace, hSpace);
vSpace = t.getDimensionPixelOffset(R.styleable.rainbowbar_rainbowbar_vspace, vSpace);
barColor = t.getColor(R.styleable.rainbowbar_rainbowbar_color, barColor);
t.recycle(); // we should always recycle after used
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(barColor);
mPaint.setStrokeWidth(vSpace);
}
.......
}
View有了三个构造方法需要我们重写,这里介绍下三个方法会被调用的场景,
第一个方法,一般我们这样使用时会被调用,View view = new View(context);
第二个方法,当我们在xml布局文件中使用View时,会在inflate布局时被调用,
<View layout_width="match_parent" layout_height="match_parent"/>。
第三个方法,跟第二种类似,但是增加style属性设置,这时inflater布局时会调用第三个构造方法。
<View style="@styles/MyCustomStyle" layout_width="match_parent" layout_height="match_parent"/>。
Ⅲ android自定义控件继承View,其中父类的三个构造方法有什么区别
在代码里new的话一般用一个参数的,
写在xml里的 调用2个参数的 attr里边传过来的是 xml里边对应的height width等参数,包括自己定义的参数,如果在xml里边写入自定义控件的话 必须要重写2个参数的构造函数
第3个参数不熟,传style的吧貌似
Ⅳ Android的RecycleView. ViewHolder的构造函数是怎样的
1、代码如下:
mAdapter.setOnItemClickListener(new NavAdapter.(){
@Override
public void onItemClick(View view , int position){
Log.v("mainDebug","position:"+position+"");
//重置显示效果
int firstItemPosition=((LinearLayoutManager)mLayoutManager).findFirstVisibleItemPosition();
int lastPos = ((LinearLayoutManager)mLayoutManager).findLastVisibleItemPosition();
System.out.println(firstItemPosition+"——"+lastPos);
for(int i=1;i<mAdapter.getItemCount();i++)
{
View v=mRecyclerView.getChildAt(i);
NavAdapter.ViewHolder viewHolder=(NavAdapter.ViewHolder)mRecyclerView.getChildViewHolder(v);
viewHolder.getmItemText().setTextColor(getResources().getColor(R.color.black));
viewHolder.getmItemIcon().setImageResource(mAdapter.getNormalIcon(i - 1));
}
//设置选中效果
View v=mRecyclerView.getChildAt(position);
NavAdapter.ViewHolder viewHolder=(NavAdapter.ViewHolder)mRecyclerView.getChildViewHolder(v);
viewHolder.getmItemText().setTextColor(getResources().getColor(R.color.mainColor));
viewHolder.getmItemIcon().setImageResource(mAdapter.getSelectedIcon(position - 1));
//提示重绘
mAdapter.notifyDataSetChanged();
}
});
2、我根据点击的position得到View,这一步得到的确实是我说点击的那个view,我已经输出view中的textView内容看过了,没有问题。
3、但是我得到ViewHolder后,去设置文本的颜色,缺发现改变的颜色不是我所点击的那个view的,而是他前面的2个的view的颜色被改变了,不知道该怎么解决。
Ⅳ android中怎么在View构造的attrs中拿到android给的属性
//Android原生的属性,都是提供方法可以获得的,当然也可以通过
attrs获得,而自定义的属性获得值方式如下,当然原生的也是一样,只需要把attr name该成系统的。
一、 首先要在res/values目录下建立一个attrs.xml(名字可以自己定义)的文件,并在此文件中增加对控件的属性的定义.其xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="my_text_view">
<attr name="text_size" format="float"></attr>
<attr name="text_color" format="color"></attr>
<attr name="text_back_ground" format="color|reference"></attr>
</declare-styleable>
</resources>
在这里,需要补充attrs属性的相关知识,即Attr属性是如何在XML中定义的,自定义属性的Value值可以有10种类型以及其类型的组合值,其具体使用方法如下:
1. reference:参考某一资源ID。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID"
/>
2. color:颜色值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>
(2)属性使用:
<TextView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"
/>
3. boolean:布尔值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
(2)属性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"
/>
4. dimension:尺寸值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
(2)属性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
/>
5. float:浮点值。
(1)属性定义:
<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
(2)属性使用:
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.7"
/>
6. integer:整型值。
(1)属性定义:
<declare-styleable name = "AnimatedRotateDrawable">
<attr name = "visible" />
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
<attr name = "pivotX" />
<attr name = "pivotY" />
<attr name = "drawable" />
</declare-styleable>
(2)属性使用:
<animated-rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
android:drawable = "@drawable/图片ID"
android:pivotX = "50%"
android:pivotY = "50%"
android:framesCount = "12"
android:frameDuration = "100"
/>
7. string:字符串。
(1)属性定义:
<declare-styleable name = "MapView">
<attr name = "apiKey" format = "string" />
</declare-styleable>
(2)属性使用:
<com.google.android.maps.MapView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:apiKey = "_bc_g"
/>
8. fraction:百分数。
(1)属性定义:
<declare-styleable name="RotateDrawable">
<attr name = "visible" />
<attr name = "fromDegrees" format = "float" />
<attr name = "toDegrees" format = "float" />
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
<attr name = "drawable" />
</declare-styleable>
(2)属性使用:
<rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
android:interpolator = "@anim/动画ID"
android:fromDegrees = "0"
android:toDegrees = "360"
android:pivotX = "200%"
android:pivotY = "300%"
android:ration = "5000"
android:repeatMode = "restart"
android:repeatCount = "infinite"
/>
9. enum:枚举值。
(1)属性定义:
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
(2)属性使用:
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>
10. flag:位或运算。
(1)属性定义:
<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" />
<flag name = "stateVisible" value = "4" />
<flag name = "stateAlwaysVisible" value = "5" />
<flag name = "adjustUnspecified" value = "0x00" />
<flag name = "adjustResize" value = "0x10" />
<flag name = "adjustPan" value = "0x20" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable>
(2)属性使用:
<activity
android:name = ".StyleAndThemeActivity"
android:label = "@string/app_name"
android:windowSoftInputMode = "stateUnspecified | stateUnchanged|stateHidden">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
注意:
属性定义时可以指定多种类型值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
(2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID|#00FF00"
/>
二、接下来实现自定义View的类,其中下面的构造方法是重点,在代码中获取自定义属性,其代码如下:
package com.example.CustomAttr;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
/**
* Created with IntelliJ IDEA.
* User: wen.nan
* Date: 13-9-28
* Time: 下午10:00
* To change this template use File | Settings | File Templates.
*/
public class CustomTextView extends TextView {
private TypedArray mTypedArray;
private Paint mPaint;
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
inial(context,attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inial(context,attrs);
}
private void inial(Context context,AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.my_text_view);
float textsize = typedArray.getFloat(R.styleable.my_text_view_text_size,14) ;
int textColor = typedArray.getColor(R.styleable.my_text_view_text_color,0xFFFFFF) ;
int bgColor = typedArray.getColor(R.styleable.my_text_view_text_back_ground,0xFFFFFF) ;
super.setTextColor(textColor);
super.setTextSize(textsize);
super.setBackgroundColor(bgColor);
typedArray.recycle();
}
}
三、接下来在XML布局中引用自定义View控件,其XML代码如下:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.example.CustomAttr.CustomTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MainActivity"
app:text_size ="20"
app:text_color ="#00FF00"
app:text_back_ground="#ffffff"
/>
</LinearLayout>
注意上面XML中代码: xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr",是自定义的app命名空间,res后面是应用程序包名,然后可以直接使用app:text_size,等属性,其值类型要和attrs.xml定义的属性Value值相对应。
四、总结:
注意该例子中是使用app:text_size = "20 和app:text_color="#00FF00定义TextView的颜色和textView的字体大小,而不是使用系统的属性android:textsize等。该例子中只是起到抛砖引玉的作用,你可以自定义其他属性,来实现你想要的自定义View效果。
例子资源下载:
http://download.csdn.net/detail/nanwen666/6346979
Ⅵ Android的ViewHolder的子类构造函数中为何要super(view)调用父类构造函数
看源码,它仅仅是给ViewHolder里的itemView赋值了。同时也做了一个判断。这里赋值之后后面会有几个方法会用到。所以如果不调用super(view),super里跟itemView有关的方法调用时会出现空指针异常。
Ⅶ Android自定义控件CustomView,构造函数的参数context是怎么获取的
很明显,这个context是在调用构造函数的时候传递进来的。
以两个参数的构造函数为例,这个一般是在xml使用该控件后,解析xml时会调用构造方法。
那么这个时候传的context怎么来的呢?从父类传过来的,父类呢,也是从父类的父类传过来的,顶层父类是Decorview,那么看看Decorview的context怎么来的。
上图ActivityThread.java里的一个方法。
创建activity实例之前,我们会先创建context,而这个context实际上就是new 了一个ContextImpl。然后和activity绑定。所以getContext实际上就是一个ContextImpl实例。
Ⅷ Android中好多类的构造方法中用Context做形参为什么呀传入Context对象呢
context所必须的动作
- 生成View
- 调用sharedpreference
- 开始activity
也就是说画面上的信息,应用程序内部的记录,以及Activity的调用等需要通过Context实现,总而言之它可以它的主要功能是加载和访问资源。它类似于“环境和背景”一样的东西。虽然有Enviroment类实现同样的类似功能,但是通常使用Context。
Context有两种,通常在类和方法间传递的是Activity Context。
http://blog.csdn.net/timchen6824/archive/2011/05/12/6414870.aspx
这里面有详细的介绍,可能不是完全针对你的问题,但是有助于理解Context。
Ⅸ android 自定义一个view并加入滚动条
自定义VIEW
class xxxx extends View{
}// 继承自view,然后会提示你选哪个构造方法,选择一个你要的构造,并重写父类方法