導航:首頁 > 操作系統 > androidview構造方法

androidview構造方法

發布時間:2022-08-23 06:24:52

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,然後會提示你選哪個構造方法,選擇一個你要的構造,並重寫父類方法

閱讀全文

與androidview構造方法相關的資料

熱點內容
單片機按鍵外部中斷 瀏覽:559
單片機的usb供電 瀏覽:255
更改android解析度 瀏覽:185
phpstaticfinal 瀏覽:694
成人倫理風月片電影 瀏覽:294
禁播愛情片 瀏覽:21
動漫電影免費版大全 瀏覽:14
java什麼是this 瀏覽:811
拍攝指南by小說製造機txt下載 瀏覽:738
豆瓣pdf 瀏覽:723
春宮妖姬演員表 瀏覽:112
韓國情愛電影在線 瀏覽:812
程序員那麼可愛顧墨吃醋 瀏覽:201
伺服器未測速怎麼辦 瀏覽:15
男主角林晨的都市小說 瀏覽:899
百度文庫系統源碼nodejs 瀏覽:997
電影院正在上映的電影怎麼在家看 瀏覽:61
永輝生活app如何使用手機閃付 瀏覽:178
吸奶的電影 瀏覽:986
對越自衛反擊戰電影大全集免費 瀏覽:565