❶ android自定義View組合控制項
需要看你實現什麼效果。一般自定義控制項可以用2種方式:
1.
將現有的控制項封裝起來,作為一個通用的組件來使用,此時只能用代碼的形式調用,無法再xml中引用,例如將textview和button封裝為一個登錄控制項。
2.
繼承view或者viewgroup,其實viewgroup也是繼承的view。然後依次實現onmeasure()、onlayout()、ondraw(),
1.
onmeasure
--
負責測繪控制項的大小
2.
onlayout
--
負責控制項中子元素擺放的位置
3.
ondraw
--
負責控制項和子控制項的繪制,使其顯示在屏幕中
4.
一些設計和實現較好的自定義view,一般還需要考慮事件的傳遞、動畫的控制、touch事件的處理等
❷ 安卓的自定義Ratingbar控制項怎麼改變星星大小
RatingBar 自定義style的用法
第一步:配置ratingbar 的style
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/food_ratingbar_full</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
第二部:配置 drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+android:id/background"
android:drawable="@drawable/fans_mark_gray" />
<item android:id="@+android:id/secondaryProgress"
android:drawable="@drawable/fans_mark_gray" />
<item android:id="@+android:id/progress"
android:drawable="@drawable/fans_mark_red" />
</layer-list>
第三部:引用 style
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"//star 數量
android:stepSize="1" //如果可以交互 每次的增量
android:isIndicator="false"//true 不可交互
android:rating="2" //默認 進度2
style="@style/foodRatingBar"
/>
❸ android 如何設置view的大小
可以在activity中定義一個LinearLayout,然後再將自定義的view加到LinearLayout中:
//假設MyView是你自定義的view
MyView mView=new MyView();
LinearLayout myLinear=new LinearLayout();
//LinearLayout.LayoutParams.WRAP_CONTENT可以設定為你需要的值
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
myLinear.addView(mView,params1);
用這個方法的話還得將setContentView()設為setContentView(myLinear);
如果這個activity中還有其他的view的話,可以通過同樣的方法將其加在myLinear上
❹ android自定義控制項,我想做一個自定義相對布局控制項中,包含兩個子TextView
Android動態改變View控制項大小的方法:
1、聲明控制項參數獲取對象 LayoutParams lp;
2、獲取控制項參數: lp = 控制項id.getLayoutParams();
3、設置控制項參數:如高度。 lp.height -= 10;
4:、使設置生效:控制項id.setLayoutParams(lp);
例如如要把Imageview下移200px: ImageView.setPadding( ImageView.getPaddingLeft(), ImageView.getPaddingTop()+200, ImageView.getPaddingRight(), ImageView.getPaddingBottom());
❺ android 自定義view 的高度,寬度怎麼設置
Android中View是顯示控制項,需要用一個layout布局來裝載,layout尺寸大寫的設置如下:
1. 盡量使用wrap_content、match_parent、weight 來規定layout的大小。
2. 要確保布局的靈活性並適應各種尺寸的屏幕,應使用 「wrap_content」 和 「match_parent」 控制某些視圖組件的寬度和高度。
3. 使用 「wrap_content」,系統就會將視圖的寬度或高度設置成所需的最小尺寸以適應視圖中的內容,而 「match_parent」(在低於 API 級別 8 的級別中稱為「fill_parent」)則會展開組件以匹配其父視圖的尺寸。
4. 如果使用 「wrap_content」 和 「match_parent」 尺寸值而不是硬編碼的尺寸,視圖就會相應地僅使用自身所需的空間或展開以填滿可用空間。此方法可讓布局正確適應各種屏幕尺寸和屏幕方向。
5. 如果某些地方必須要規定控制項的大小,那麼要使用dp 或者 dip單位。
❻ android自定義控制項怎麼用
開發自定義控制項的步驟:
1、了解View的工作原理
2、 編寫繼承自View的子類
3、 為自定義View類增加屬性
4、 繪制控制項
5、 響應用戶消息
6 、自定義回調函數
一、View結構原理
Android系統的視圖結構的設計也採用了組合模式,即View作為所有圖形的基類,Viewgroup對View繼承擴展為視圖容器類。
View定義了繪圖的基本操作
基本操作由三個函數完成:measure()、layout()、draw(),其內部又分別包含了onMeasure()、onLayout()、onDraw()三個子方法。具體操作如下:
1、measure操作
measure操作主要用於計算視圖的大小,即視圖的寬度和長度。在view中定義為final類型,要求子類不能修改。measure()函數中又會調用下面的函數:
(1)onMeasure(),視圖大小的將在這里最終確定,也就是說measure只是對onMeasure的一個包裝,子類可以覆寫onMeasure()方法實現自己的計算視圖大小的方式,並通過setMeasuredDimension(width, height)保存計算結果。
2、layout操作
layout操作用於設置視圖在屏幕中顯示的位置。在view中定義為final類型,要求子類不能修改。layout()函數中有兩個基本操作:
(1)setFrame(l,t,r,b),l,t,r,b即子視圖在父視圖中的具體位置,該函數用於將這些參數保存起來;
(2)onLayout(),在View中這個函數什麼都不會做,提供該函數主要是為viewGroup類型布局子視圖用的;
3、draw操作
draw操作利用前兩部得到的參數,將視圖顯示在屏幕上,到這里也就完成了整個的視圖繪制工作。子類也不應該修改該方法,因為其內部定義了繪圖的基本操作:
(1)繪制背景;
(2)如果要視圖顯示漸變框,這里會做一些准備工作;
(3)繪制視圖本身,即調用onDraw()函數。在view中onDraw()是個空函數,也就是說具體的視圖都要覆寫該函數來實現自己的顯示(比如TextView在這里實現了繪制文字的過程)。而對於ViewGroup則不需要實現該函數,因為作為容器是「沒有內容「的,其包含了多個子view,而子View已經實現了自己的繪制方法,因此只需要告訴子view繪制自己就可以了,也就是下面的dispatchDraw()方法;
(4)繪制子視圖,即dispatchDraw()函數。在view中這是個空函數,具體的視圖不需要實現該方法,它是專門為容器類准備的,也就是容器類必須實現該方法;
(5)如果需要(應用程序調用了setVerticalFadingEdge或者setHorizontalFadingEdge),開始繪制漸變框;
(6)繪制滾動條;
從上面可以看出自定義View需要最少覆寫onMeasure()和onDraw()兩個方法。
二、View類的構造方法
創建自定義控制項的3種主要實現方式:
1)繼承已有的控制項來實現自定義控制項: 主要是當要實現的控制項和已有的控制項在很多方面比較類似, 通過對已有控制項的擴展來滿足要求。
2)通過繼承一個布局文件實現自定義控制項,一般來說做組合控制項時可以通過這個方式來實現。
注意此時不用onDraw方法,在構造廣告中通過inflater載入自定義控制項的布局文件,再addView(view),自定義控制項的圖形界面就載入進來了。
3)通過繼承view類來實現自定義控制項,使用GDI繪制出組件界面,一般無法通過上述兩種方式來實現時用該方式。
三、自定義View增加屬性的兩種方法:
1)在View類中定義。通過構造函數中引入的AttributeSet 去查找XML布局的屬性名稱,然後找到它對應引用的資源ID去找值。
案例:實現一個帶文字的圖片(圖片、文字是onDraw方法重繪實現)
public class MyView extends View {
private String mtext;
private int msrc;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
int resourceId = 0;
int textId = attrs.getAttributeResourceValue(null, "Text",0);
int srcId = attrs.getAttributeResourceValue(null, "Src", 0);
mtext = context.getResources().getText(textId).toString();
msrc = srcId;
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.RED);
InputStream is = getResources().openRawResource(msrc);
Bitmap mBitmap = BitmapFactory.decodeStream(is);
int bh = mBitmap.getHeight();
int bw = mBitmap.getWidth();
canvas.drawBitmap(mBitmap, 0,0, paint);
//canvas.drawCircle(40, 90, 15, paint);
canvas.drawText(mtext, bw/2, 30, paint);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.myimageview2.MyView
android:id="@+id/myView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Text="@string/hello_world"
Src="@drawable/xh"/>
</LinearLayout>
屬性Text, Src在自定義View類的構造方法中讀取。
2)通過XML為View注冊屬性。與Android提供的標准屬性寫法一樣。
案例: 實現一個帶文字說明的ImageView (ImageView+TextView組合,文字說明,可在布局文件中設置位置)
public class MyImageView extends LinearLayout {
public MyImageView(Context context) {
super(context);
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
int resourceId = -1;
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.MyImageView);
ImageView iv = new ImageView(context);
TextView tv = new TextView(context);
int N = typedArray.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.MyImageView_Oriental:
resourceId = typedArray.getInt(
R.styleable.MyImageView_Oriental, 0);
this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL
: LinearLayout.VERTICAL);
break;
case R.styleable.MyImageView_Text:
resourceId = typedArray.getResourceId(
R.styleable.MyImageView_Text, 0);
tv.setText(resourceId > 0 ? typedArray.getResources().getText(
resourceId) : typedArray
.getString(R.styleable.MyImageView_Text));
break;
case R.styleable.MyImageView_Src:
resourceId = typedArray.getResourceId(
R.styleable.MyImageView_Src, 0);
iv.setImageResource(resourceId > 0 ?resourceId:R.drawable.ic_launcher);
break;
}
}
addView(iv);
addView(tv);
typedArray.recycle();
}
}
attrs.xml進行屬性聲明, 文件放在values目錄下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyImageView">
<attr name="Text" format="reference|string"></attr>
<attr name="Oriental" >
<enum name="Horizontal" value="1"></enum>
<enum name="Vertical" value="0"></enum>
</attr>
<attr name="Src" format="reference|integer"></attr>
</declare-styleable>
</resources>
MainActivity的布局文件:先定義命名空間 xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2" (com.example.myimageview2為你
在manifest中定義的包名)
然後可以像使用系統的屬性一樣使用:uview:Oriental="Vertical"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<com.example.myimageview2.MyImageView
android:id="@+id/myImageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
uview:Text="這是一個圖片說明"
uview:Src="@drawable/tw"
uview:Oriental="Vertical">
</com.example.myimageview2.MyImageView>
</LinearLayout>
四、控制項繪制 onDraw()
五、
六:自定義View的方法
onFinishInflate() 回調方法,當應用從XML載入該組件並用它構建界面之後調用的方法
onMeasure() 檢測View組件及其子組件的大小
onLayout() 當該組件需要分配其子組件的位置、大小時
onSizeChange() 當該組件的大小被改變時
onDraw() 當組件將要繪制它的內容時
onKeyDown 當按下某個鍵盤時
onKeyUp 當松開某個鍵盤時
onTrackballEvent 當發生軌跡球事件時
onTouchEvent 當發生觸屏事件時
onWindowFocusChanged(boolean) 當該組件得到、失去焦點時
onAtrrachedToWindow() 當把該組件放入到某個窗口時
onDetachedFromWindow() 當把該組件從某個窗口上分離時觸發的方法
onWindowVisibilityChanged(int): 當包含該組件的窗口的可見性發生改變時觸發的方法
❼ android 自定義view 怎麼設置其寬度和高度
自定義view的父類肯定是View或者View的子類。所以也是支持layout_width,layout_height屬性的,如果想另外控制的話,可以重寫View的onDraw方法。
❽ Android 自定義控制項 動態設置高度
Android動態改變View控制項大小的方法:
1、聲明控制項參數獲取對象 LayoutParams lp;
2、獲取控制項參數: lp = 控制項id.getLayoutParams();
3、設置控制項參數:如高度。 lp.height -= 10;
4:、使設置生效:控制項id.setLayoutParams(lp);
例如如要把Imageview下移200px: ImageView.setPadding( ImageView.getPaddingLeft(), ImageView.getPaddingTop()+200, ImageView.getPaddingRight(), ImageView.getPaddingBottom());
❾ Android 自定義控制項開發中 onLayout如何設置裡面控制項的大小(Android 大神請進……)
ViewGroup在onLayout函數中通過調用其children的layout函數來設置子視圖相對與父視圖中的位置,具體位置由函數layout的參數決定,當我們繼承ViewGroup時必須重載onLayout函數(ViewGroup中onLayout是abstract修飾),然而onMeasure並不要求必須重載,因為相對與layout來說,measure過程並不是必須的,具體後面會提到。首先我們來看下View.java中函數layout和onLayout的源碼:
public void layout(int l, int t, int r, int b) {
int oldL = mLeft;
int oldT = mTop;
int oldB = mBottom;
int oldR = mRight;
boolean changed = setFrame(l, t, r, b);
if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
}
onLayout(changed, l, t, r, b);
mPrivateFlags &= ~LAYOUT_REQUIRED;
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnLayoutChangeListeners != null) {
ArrayList<OnLayoutChangeListener> listenersCopy =
(ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
int numListeners = listenersCopy.size();
for (int i = 0; i < numListeners; ++i) {
listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
}
}
}
mPrivateFlags &= ~FORCE_LAYOUT;
}
函數layout的主體過程還是很容易理解的,首先通過調用setFrame函數來對4個成員變數(mLeft,mTop,mRight,mBottom)賦值,然後回調onLayout函數,最後回調所有注冊過的listener的onLayoutChange函數。
對於View來說,onLayout只是一個空實現,一般情況下我們也不需要重載該函數:
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
}
接著我們來看下ViewGroup.java中layout的源碼:
public final void layout(int l, int t, int r, int b) {
if (mTransition == null || !mTransition.isChangingLayout()) {
super.layout(l, t, r, b);
} else {
// record the fact that we noop'd it; request layout when transition finishes
mLayoutSuppressed = true;
}
}
super.layout(l, t, r, b)調用的即是View.java中的layout函數,相比之下ViewGroup增加了LayoutTransition的處理,LayoutTransition是用於處理ViewGroup增加和刪除子視圖的動畫效果,也就是說如果當前ViewGroup未添加LayoutTransition動畫,或者LayoutTransition動畫此刻並未運行,那麼調用super.layout(l, t, r, b),繼而調用到ViewGroup中的onLayout,否則將mLayoutSuppressed設置為true,等待動畫完成時再調用requestLayout()。
上面super.layout(l, t, r, b)會調用到ViewGroup.java中onLayout,其源碼實現如下:
@Override
protected abstract void onLayout(boolean changed,
int l, int t, int r, int b);
和前面View.java中的onLayout實現相比,唯一的差別就是ViewGroup中多了關鍵字abstract的修飾,也就是說ViewGroup類只能用來被繼承,無法實例化,並且其子類必須重載onLayout函數,而重載onLayout的目的就是安排其children在父視圖的具體位置。重載onLayout通常做法就是起一個for循環調用每一個子視圖的layout(l, t, r, b)函數,傳入不同的參數l, t, r, b來確定每個子視圖在父視圖中的顯示位置。
那layout(l, t, r, b)中的4個參數l, t, r, b如何來確定呢?聯想到之前的measure過程,measure過程的最終結果就是確定了每個視圖的mMeasuredWidth和mMeasuredHeight,這兩個參數可以簡單理解為視圖期望在屏幕上顯示的寬和高,而這兩個參數為layout過程提供了一個很重要的依據(但不是必須的),為了說明這個過程,我們來看下LinearLayout的layout過程:
void layoutVertical() {
……
for (int i = 0; i < count; i++) {
final View child = getVirtualChildAt(i);
if (child == null) {
childTop += measureNullChild(i);
} else if (child.getVisibility() != GONE) {
final int childWidth = child.getMeasuredWidth();
final int childHeight = child.getMeasuredHeight();
……
setChildFrame(child, childLeft, childTop + getLocationOffset(child),
childWidth, childHeight);
childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child);
i += getChildrenSkipCount(child, i);
}
}
}
private void setChildFrame(View child, int left, int top, int width, int height) {
child.layout(left, top, left + width, top + height);
}
從setChildFrame可以看到LinearLayout中的子視圖的右邊界等於left + width,下邊界等於top+height,也就是說在LinearLayout中其子視圖顯示的寬和高由measure過程來決定的,因此measure過程的意義就是為layout過程提供視圖顯示範圍的參考值。
layout過程必須要依靠measure計算出來的mMeasuredWidth和mMeasuredHeight來決定視圖的顯示大小嗎?事實並非如此,layout過程中的4個參數l, t, r, b完全可以由視圖設計者任意指定,而最終視圖的布局位置和大小完全由這4個參數決定,measure過程得到的mMeasuredWidth和mMeasuredHeight提供了視圖大小的值,但我們完全可以不使用這兩個值,可見measure過程並不是必須的。\\
說到這里就不得不提getWidth()、getHeight()和getMeasuredWidth()、getMeasuredHeight()這兩對函數之間的區別,getMeasuredWidth()、getMeasuredHeight()返回的是measure過程得到的mMeasuredWidth和mMeasuredHeight的值,而getWidth()和getHeight()返回的是mRight - mLeft和mBottom - mTop的值,看View.java中的源碼便一清二楚了:
public final int getMeasuredWidth() {
return mMeasuredWidth & MEASURED_SIZE_MASK;
}
public final int getWidth() {
return mRight - mLeft;
}
這也解釋了為什麼有些情況下getWidth()和getMeasuredWidth()以及getHeight()和getMeasuredHeight()會得到不同的值。