當前位置:首頁 » 安卓系統 » android自定義view控制項

android自定義view控制項

發布時間: 2022-06-10 11:27:25

『壹』 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 :

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:
<?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中,直接上代碼
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 studio中實現自定義view

一、首先新建一個項目,項目及名稱自擬。
二、在app上點擊右鍵->new->Mole 選擇Android library。
三、在topbar下的values中新建一個attrs.xml文件,用來存放自定義view的屬性。
4.在topbar下實現view。
5.上面兩部做完後就是引用這個view,這里需要注意的是要在主app的build.gradle中添加引用如下:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':topbar')
}
topbar就是要使用的moudle,切記添加引用。然後就可以使用了。
6.要想使用自定義view中的屬性的話任然需要添加xmlns:custom="schemas.android.com/apk/res-auto",前面加上http。
在這里還要注意命名空間也就是xustom之前一定不能定義過,否則重復的話就無法使用。

『叄』 怎樣在Android Menu item中使用自定義View

1.自定義屬性:attrs.xml

2.MenuItemView.java源碼

package com.dandy.widget;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.lingyun.switchbutton.R;

/**
* 設置,菜單中的布局項
* 默認控制項是縱向居中顯示,所有paddingTop和paddingBottom在這沒有作用
* Created by dandy on 2016/4/14.
*/
public class MenuItemView extends View{

private static final String TAG = "MenuItemView";

/**默認是按下狀態的最小值**/
private static final long PRESSED_TIMEOUT = 10;

/**默認控制項距離邊界的大小**/
private static final int PADDING_DEFAULT = 18;

/**默認控制項的高**/
private static final int HEIGHT_DEFAULT = 50;

/**文字繪制時默認大小**/
private static final int TEXTSZIE_DEFAULT = 14;

/**尾部箭頭圖標大小**/
private static final int ARROW_SIZE = 13;

/***SwitchButton默認寬*/
private static final int SWITCHBUTTON_WIDTH = 50;

/***SwitchButton默認高*/
private static final int SWITCHBUTTON_HEIGHT = 28;

/**頭標**/
private Drawable headerDrawable;

/**頭標寬**/
private int headerDrawableWidth;
/**頭標高**/
private int headerDrawableHeight;

/**距離左邊緣的距離**/
private int paddingLeft;

/**距離右邊緣的距離**/
private int paddingRight;

/**繪制頭標時,畫布在Y軸的繪制偏移量**/
private float headerDrawableStartDrawY;

/**文字與圖片間的距離**/
private int drawablePadding = -1;

/**頭部文字提示**/
private String textHeader;

/**文字顏色**/
private int textHeaderColor = Color.parseColor("#5a5a5a");

/**文字大小**/
private int textSize = -1;

/**文字繪制時,畫布在Y軸的繪制偏移量**/
private float textStartDrawY;

/**繪制文字的畫筆**/
private Paint textPaint;

/** 尾部 > 圖片**/
private Drawable arrowDrawable;
/**尾部 > 大小**/
private int arrowSize = -1;

/** > 繪制的X軸偏移量**/
private float arrowStartDrawX;

/** > 繪制的Y軸偏移量**/
private float arrowStartDrawY;

/**footerDrawable != null 時,繪制的時候是否按照原圖片大小繪制**/
private boolean arrowWropToSelf = true;

/**尾部寬**/
private int arrowDrawableWidth;
/**尾部高**/
private int arrowDrawableHeight;

/**繪制arrow畫筆**/
private Paint arrowPaint;

/**arrowPaint 顏色**/
private int arrowColor = Color.parseColor("#5a5a5a");

private DisplayMetrics dm;

/*以下是繪制SwitchButton所用到的參數*/

private Style style = Style.CUSTOM_ITEM;

/**默認寬**/
private int switchButtonWidth = -1;

/**默認高**/
private int switchButtonHeight = -1;

private static final long DELAYDURATION = 10;

/**開啟顏色**/
private int onColor = Color.parseColor("#4ebb7f");
/**關閉顏色**/
private int offColor = Color.parseColor("#dadbda");
/**灰色帶顏色**/
private int areaColor = Color.parseColor("#dadbda");
/**手柄顏色**/
private int handlerColor = Color.parseColor("#ffffff");
/**邊框顏色**/
private int borderColor = offColor;
/**開關狀態**/
private boolean toggleOn = false;
/**邊框寬**/
private int borderWidth = 2;
/**縱軸中心**/
private float centerY;
/**按鈕水平方向開始、結束的位置**/
private float startX,endX;
/**手柄x軸方向最小、最大值**/
private float handlerMinX,handlerMaxX;
/**手柄大小**/
private int handlerSize;
/**手柄在x軸的坐標位置**/
private float handlerX;
/**關閉時內部灰色帶寬度**/
private float areaWidth;
/**是否使用動畫效果**/
private boolean animate = true;
/**是否默認處於打開狀態**/
private boolean defaultOn = true;
/**按鈕半徑**/
private float radius;

/**整個switchButton的區域**/
private RectF switchRectF = new RectF();

/**繪制switchButton的畫筆**/
private Paint switchPaint;

private OnToggleChangedListener mListener;

private Handler mHandler = new Handler();

private double currentDelay;

private float downX = 0;

/**switchButton在X軸繪制的偏移量**/
private float switchButtonDrawStartX;

/**switchButton在Y軸繪制的偏移量**/
private float switchButtonDrawStartY;

/**分割線,默認在底部繪制**/
private Drawable dividerr;

/**分割線繪制的寬**/
private int dividerWidth = 2;

/**是否需要繪制分割線**/
private boolean dividerVisibilty = true;

/**觸摸事件是否完成**/
private boolean touchDownEnd = false;

public MenuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setup(attrs);
}
public MenuItemView(Context context, AttributeSet attrs) {
super(context, attrs);
setup(attrs);
}

/**
* 初始化控制項,獲取相關的控制項屬性
* @param attrs
*/
private void setup(AttributeSet attrs){

dm = Resources.getSystem().getDisplayMetrics();

TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MenuItemView);
if(typedArray != null){
int count = typedArray.getIndexCount();
for(int i = 0;i < count;i++){
int attr = typedArray.getIndex(i);
switch (attr){
case R.styleable.MenuItemView_headerDrawable:
headerDrawable = typedArray.getDrawable(attr);
break;
case R.styleable.MenuItemView_drawPadding:
drawablePadding = typedArray.getDimensionPixelSize(attr,drawablePadding);
break;
case R.styleable.MenuItemView_textHeader:
textHeader = typedArray.getString(attr);
break;
case R.styleable.MenuItemView_textHeaderColor:
textHeaderColor = typedArray.getColor(attr, textHeaderColor);
break;
case R.styleable.MenuItemView_textSize:
textSize = typedArray.getDimensionPixelSize(attr, textSize);
break;
case R.styleable.MenuItemView_arrowDrawable:
arrowDrawable = typedArray.getDrawable(attr);
break;
case R.styleable.MenuItemView_arrowSize:
arrowSize = typedArray.getDimensionPixelSize(attr, arrowSize);
break;
case R.styleable.MenuItemView_arrowWropToSelf:
arrowWropToSelf = typedArray.getBoolean(attr, true);
break;
case R.styleable.MenuItemView_arrowColor:
arrowColor = typedArray.getColor(attr, arrowColor);
break;
case R.styleable.MenuItemView_onColor:
onColor = typedArray.getColor(attr, onColor);
break;
case R.styleable.MenuItemView_offColor:
borderColor = offColor = typedArray.getColor(attr,offColor);
break;
case R.styleable.MenuItemView_areaColor:
areaColor = typedArray.getColor(attr, areaColor);
break;
case R.styleable.MenuItemView_handlerColor:
handlerColor = typedArray.getColor(attr, handlerColor);
break;
case R.styleable.MenuItemView_bordeWidth:
borderWidth = typedArray.getColor(attr, borderWidth);
break;
case R.styleable.MenuItemView_animate:
animate = typedArray.getBoolean(attr, animate);
break;
case R.styleable.MenuItemView_defaultOn:
defaultOn = typedArray.getBoolean(attr, defaultOn);
break;
case R.styleable.MenuItemView_Style:
style = Style.getValue(typedArray.getInt(attr, Style.CUSTOM_ITEM.ordinal()));
break;
case R.styleable.MenuItemView_switchButtonWidth:
switchButtonWidth = typedArray.getDimensionPixelOffset(attr, switchButtonWidth);
break;
case R.styleable.MenuItemView_switchButtonHeight:
switchButtonHeight = typedArray.getDimensionPixelOffset(attr, switchButtonHeight);
break;
case R.styleable.MenuItemView_dividerr:
dividerr = typedArray.getDrawable(attr);
break;
case R.styleable.MenuItemView_dividerWidth:
dividerWidth = typedArray.getDimensionPixelOffset(attr,dividerWidth);
break;
case R.styleable.MenuItemView_dividerVisibilty:
dividerVisibilty = typedArray.getBoolean(attr,dividerVisibilty);
break;
}
}
typedArray.recycle();
}

『肆』 android自定義view要怎麼使用

視圖,凡事能被用戶看到的小控制項都是一種view,也可以自定義view

『伍』 Android開發 自定義View

Android自定義View實現很簡單:
1、繼承View,重寫構造函數、onDraw,(onMeasure)等函數。
2、如果自定義的View需要有自定義的屬性,需要在values下建立attrs.xml。在其中定義你的屬性。
3、在使用到自定義View的xml布局文件中需要加入xmlns:前綴="http://schemas.android.com/apk/res/你的自定義View所在的包路徑".
4、在使用自定義屬性的時候,使用前綴:屬性名,如my:textColor="#FFFFFFF"。
實例:
自定義TextView類:
復制代碼
package com.zst.service.component;
import com.example.hello_wangle.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MyTextView extends TextView {
//不能在布局文件中使用
public MyTextView(Context context) {
super(context);
}

//布局文件中用到此構造函數

『陸』 android 自定義view怎麼做能提高效率

自定義view一般要調用裡面的onDraw()方法,提高效率個人認為,主要是圖片載入釋放的一些處理很重要。比如,有些圖可以在構造函數里載入,有些可以根據具體要求在onDraw判斷一下在載入。這樣就避免了有些功能不需要上來就載入圖,但是你上來就載入大圖,很影響效率。在一個就是及時釋放相應圖片了。

『柒』 android開發怎麼用自定義view類的方法

建一個新的class
extends
View
裡面的方法自定義
這就是自定義view控制項
引用的時候
在布局裡
<包名.剛才建的類名
其他屬性>
包名.剛才建的類名

『捌』 android怎麼自定義view

{
publicCustomTextview(Contextcontext){
this(context,null);
}

publicCustomTextview(Contextcontext,AttributeSetattrs){
this(context,attrs,com.android.internal.R.attr.textViewStyle);
}

publicCustomTextview(Contextcontext,AttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
//setText("Fuckme");
}

直接可以在xml布局文件中,調用。

『玖』 android的自定義View的實現原理哪位能給我個思路呢。謝謝。

如果說要按類型來劃分的話,自定義View的實現方式大概可以分為三種,自繪控制項、組合控制項、以及繼承控制項。那麼下面我們就來依次學習一下,每種方式分別是如何自定義View的。

一、自繪控制項

自繪控制項的意思就是,這個View上所展現的內容全部都是我們自己繪制出來的。繪制的代碼是寫在onDraw()方法中的,而這部分內容我們已經在Android視圖繪制流程完全解析,帶你一步步深入了解View(二)中學習過了。

下面我們准備來自定義一個計數器View,這個View可以響應用戶的點擊事件,並自動記錄一共點擊了多少次。新建一個CounterView繼承自View,代碼如下所示:

<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ffcb05">

<Button
android:id="@+id/button_left"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@drawable/back_button"
android:text="Back"
android:textColor="#fff"/>

<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="ThisisTitle"
android:textColor="#fff"
android:textSize="20sp"/>

</RelativeLayout>

在這個布局文件中,我們首先定義了一個RelativeLayout作為背景布局,然後在這個布局裡定義了一個Button和一個TextView,Button就是標題欄中的返回按鈕,TextView就是標題欄中的顯示的文字。

接下來創建一個TitleView繼承自FrameLayout,代碼如下所示:

{

privateButtonleftButton;

privateTextViewtitleText;

publicTitleView(Contextcontext,AttributeSetattrs){
super(context,attrs);
LayoutInflater.from(context).inflate(R.layout.title,this);
titleText=(TextView)findViewById(R.id.title_text);
leftButton=(Button)findViewById(R.id.button_left);
leftButton.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
((Activity)getContext()).finish();
}
});
}

publicvoidsetTitleText(Stringtext){
titleText.setText(text);
}

publicvoidsetLeftButtonText(Stringtext){
leftButton.setText(text);
}

(OnClickListenerl){
leftButton.setOnClickListener(l);
}

}

TitleView中的代碼非常簡單,在TitleView的構建方法中,我們調用了LayoutInflater的inflate()方法來載入剛剛定義的title.xml布局,這部分內容我們已經在Android LayoutInflater原理分析,帶你一步步深入了解View(一)這篇文章中學習過了。

接下來調用findViewById()方法獲取到了返回按鈕的實例,然後在它的onClick事件中調用finish()方法來關閉當前的Activity,也就相當於實現返回功能了。

另外,為了讓TitleView有更強地擴展性,我們還提供了setTitleText()、setLeftButtonText()、setLeftButtonListener()等方法,分別用於設置標題欄上的文字、返回按鈕上的文字、以及返回按鈕的點擊事件。

到了這里,一個自定義的標題欄就完成了,那麼下面又到了如何引用這個自定義View的部分,其實方法基本都是相同的,在布局文件中添加如下代碼:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.example.customview.TitleView
android:id="@+id/title_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.customview.TitleView>

</RelativeLayout>

這樣就成功將一個標題欄控制項引入到布局文件中了,運行一下程序。

現在點擊一下Back按鈕,就可以關閉當前的Activity了。如果你想要修改標題欄上顯示的內容,或者返回按鈕的默認事件,只需要在Activity中通過findViewById()方法得到TitleView的實例,然後調用setTitleText()、setLeftButtonText()、setLeftButtonListener()等方法進行設置就OK了。

『拾』 Android自定義控制項CustomView,構造函數的參數context是怎麼獲取的

很明顯,這個context是在調用構造函數的時候傳遞進來的。

以兩個參數的構造函數為例,這個一般是在xml使用該控制項後,解析xml時會調用構造方法。

那麼這個時候傳的context怎麼來的呢?從父類傳過來的,父類呢,也是從父類的父類傳過來的,頂層父類是Decorview,那麼看看Decorview的context怎麼來的。

上圖ActivityThread.java里的一個方法。

創建activity實例之前,我們會先創建context,而這個context實際上就是new 了一個ContextImpl。然後和activity綁定。所以getContext實際上就是一個ContextImpl實例。

熱點內容
內置存儲卡可以拆嗎 發布:2025-05-18 04:16:35 瀏覽:335
編譯原理課時設置 發布:2025-05-18 04:13:28 瀏覽:378
linux中進入ip地址伺服器 發布:2025-05-18 04:11:21 瀏覽:612
java用什麼軟體寫 發布:2025-05-18 03:56:19 瀏覽:32
linux配置vim編譯c 發布:2025-05-18 03:55:07 瀏覽:107
砸百鬼腳本 發布:2025-05-18 03:53:34 瀏覽:944
安卓手機如何拍視頻和蘋果一樣 發布:2025-05-18 03:40:47 瀏覽:740
為什麼安卓手機連不上蘋果7熱點 發布:2025-05-18 03:40:13 瀏覽:803
網卡訪問 發布:2025-05-18 03:35:04 瀏覽:511
接收和發送伺服器地址 發布:2025-05-18 03:33:48 瀏覽:371