導航:首頁 > 操作系統 > androidviewindex

androidviewindex

發布時間:2022-10-10 02:10:49

A. 如何獲取addview的index-android開發問答

你好,很高興為你解答
給要添加的view中設置view.setTag(index);
index就是viewGroup的子view的個數,這樣通過getTag()就能獲取到index了

B. android ViewPager設置適配器時,viewPager.addView(view, 0); 0是什麼意思

這個方法繼承自ViewGroup,android子View在父View中都有一定順序排列,序號就用一個int值表示,index這個參數,源碼注釋是這么說的:the position at which to add the child,意思是就是子View在父View中的序號,0意思就是把這個View添加到父viewPager的第0個位置,下面是方法源碼帶注釋,可以看一下

java">/**
*Addsachildview.,the
*.
*
*<p><strong>Note:</strong>donotinvokethismethodfrom
*{@link#draw(android.graphics.Canvas)},{@link#onDraw(android.graphics.Canvas)},
*{@link#dispatchDraw(android.graphics.Canvas)}oranyrelatedmethod.</p>
*
*@paramchildthechildviewtoadd
*@
*
*@see#generateDefaultLayoutParams()
*/
publicvoidaddView(Viewchild,intindex){
LayoutParamsparams=child.getLayoutParams();
if(params==null){
params=generateDefaultLayoutParams();
if(params==null){
("generateDefaultLayoutParams()cannotreturnnull");
}
}
addView(child,index,params);
}

C. android 怎麼計算一個view的嵌套層級

層級關系的布局有兩種解決方法:

一種方法是使用標簽的自然順序和嵌套順序來形成合理的布局。

而第一種方法卻不是萬能的,有時候我們需要將原本位於下層的元素移到另一個元素上方,那可以使用z-index;

而你說的不好用,則是zindex沒有生效,沒生效就是沒有定義position屬性,如果不想讓元素的位置有所變化,就給賦予z-index屬性的元素加上相對定位的position:relative;

D. 如何獲取addview的index-Android開發問答

給要添加的view中設置view.setTag(index);index就是viewGroup的子view的個數,這樣通過getTag()就能獲取到index了

E. android動態添加view,怎麼識別每一行的view

LinearLayout是線性布局控制項,它包含的子控制項將以橫向或豎向的方式排列,按照相對位置來排列所有的widgets或者其他的containers,超過邊界時,某些控制項將缺失或消失。因此一個垂直列表的每一行只會有一個widget或者是container,而不管他們有多寬,而一個水平列表將會只有一個行高(高度為最高子控制項的高度加上邊框高度)。LinearLayout保持其所包含的widget或者是container之間的間隔以及互相對齊(相對一個控制項的右對齊、中間對齊或者左對齊)1.常用的xml屬性android:baselineAligned:是否允許用戶調整它內容的基線。android:baselineAlignedChildIndex:當一個線性布局與另一個布局是按基線對齊的一部分,它可以指定其內容的基線對齊方式。android:gravity:指定如何在該對象中放置此對象的內容(x/y坐標值)。android:orientation:設置它內容的對其方向(橫向/豎向)。gravity這個英文單詞是重心的意思,在這里就表示停靠位置的意思。android:layout_gravity和android:gravity的區別從名字上可以看到,android:gravity是對元素本身說的,元素本身的文本顯示在什麼地方靠著換個屬性設置,不過不設置默認是在左側的。android:layout_gravity是相對與它的父元素說的,說明元素顯示在父元素的什麼位置。比如說button:android:layout_gravity表示按鈕在界面上的位置。android:gravity表示button上的字在button上的位置。可選值這兩個屬性可選的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。而且這些屬性是可以多選的,用「|」分開。默認這個的值是:Gravity.LEFT2.對於LinearLayout何時生效的問題當android:orientation="vertical"時,只有水平方向的設置才起作用,垂直方向的設置不起作用。即:left,right,center_horizontal是生效的。當android:orientation="horizontal"時,只有垂直方向的設置才起作用,水平方向的設置不起作用。即:top,bottom,center_vertical是生效的。3.LinearLayout還支持為其包含的widget或者是container指定填充權值。好處就是允許其包含的widget或者是container可以填充屏幕上的剩餘空間。這也避免了在一個大屏幕中,一串widgets或者是containers擠成一堆的情況,而是允許他們放大填充空白。剩餘的空間會按這些widgets或者是containers指定的權值比例分配屏幕。默認的weight值為0,表示按照widgets或者是containers實際大小來顯示,若高於0的值,則將Container剩餘可用空間分割,分割大小具體取決於每一個widget或者是container的layout_weight及該權值在所有widgets或者是containers中的比例。例如,如果有三個文本框,其中兩個指定的權值為1,那麼,這兩個文本框將等比例地放大,並填滿剩餘的空間,而第三個文本框不會放大,按實際大小來顯示。如果前兩個文本框的取值一個為2,一個為1,顯示第三個文本框後剩餘的空間的2/3給權值為2的,1/3大小給權值為1的。也就是權值越大,重要度越大。如果LinearLayout包含子LinearLayout,子LinearLayout之間的權值越大的,重要度則越小。如果有LinearLayoutA包含LinearLayoutC,D,C的權值為2,D的權值為1,則屏幕的2/3空間分給權值為1的D,1/3分給權值為2的C。在LinearLayout嵌套的情況下,子LinearLayout必須要設置權值,否則默認的情況是未設置權值的子LinearLayout占據整個屏幕

F. 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"/>。

G. 如何獲取addview的index-Android開發問答

你好,很高興為你解答
給要添加的view中設置view.setTag(index); index就是viewGroup的子view的個數,這樣通過getTag()就能獲取到index了

~如果你認可我的回答,請及時點擊【採納為滿意回答】按鈕
~~手機提問的朋友在客戶端右上角評價點【滿意】即可。
~你的採納是我前進的動力
~~O(∩_∩)O,記得好評和採納,互相幫助,謝謝。

H. android動態載入view一個一個刷新怎麼實現

先看一下結構圖

可以看到ViewSwitcher和ViewFlipper都是繼承自ViewAnimator。


下面通過一個Demo了解一下ViewFlipper的用法

main.xml

Html代碼

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="htt p:/ /schemas.andro id.co m/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<ViewFlipper

android:id="@+id/viewFlipper"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<include

android:id="@+id/layout01"

layout="@layout/layout01" />

<include

android:id="@+id/layout02"

layout="@layout/layout02" />

</ViewFlipper>

</LinearLayout>


layout01.xml

Html代碼

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="htt p:/ /schem as.andro id.c om/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center"

android:text="一個TextView"

android:textSize="40dip" />

</LinearLayout>


layout02.xml

Html代碼

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="htt p://schema s.andro id.c om/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center"

android:orientation="vertical" >

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="一個TextView + 一個ImageView"

android:textSize="20dip" />

</LinearLayout>

</LinearLayout>


ViewFlipperDemoActivity.java

Java代碼

package com.tianjf;

import android.app.Activity;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnTouchListener;

import android.view.animation.AnimationUtils;

import android.widget.ViewFlipper;

public class ViewFlipperDemoActivity extends Activity implements

OnTouchListener {

private ViewFlipper viewFlipper;

// 左右滑動時手指按下的X坐標

private float touchDownX;

// 左右滑動時手指松開的X坐標

private float touchUpX;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);

viewFlipper.setOnTouchListener(this);

}


}

float XFrom = e1.getX();

float XTo = e2.getX();

float YFrom = e1.getY();

float YTo = e2.getY();

// 左右滑動的X軸幅度大於100,並且X軸方向的速度大於100

if (Math.abs(XFrom - XTo) > 100.0f && Math.abs(speedX) > 100.0f) {

// X軸幅度大於Y軸的幅度

if (Math.abs(XFrom - XTo) >= Math.abs(YFrom - YTo)) {

if (XFrom > XTo) {

// 下一個

mOnFlingListener.flingToNext();

} else {

// 上一個

mOnFlingListener.flingToPrevious();

}

}

} else {

return false;

}

return true;

}

public interface OnFlingListener {

void flingToNext();

void flingToPrevious();

}

}


MyViewFlipper.java

Java代碼

package com.tianjf;

import com.tianjf.MyGestureListener.OnFlingListener;

import android.content.Context;

import android.util.AttributeSet;

import android.view.GestureDetector;

import android.view.MotionEvent;

import android.view.View;

import android.widget.ViewFlipper;

public class MyViewFlipper extends ViewFlipper implements OnFlingListener {

private GestureDetector mGestureDetector = null;

private OnViewFlipperListener mOnViewFlipperListener = null;

public MyViewFlipper(Context context) {

super(context);

}

public MyViewFlipper(Context context, AttributeSet attrs) {

super(context, attrs);

}

public void setOnViewFlipperListener(OnViewFlipperListener mOnViewFlipperListener) {

this.mOnViewFlipperListener = mOnViewFlipperListener;

MyGestureListener myGestureListener = new MyGestureListener();

myGestureListener.setOnFlingListener(this);

mGestureDetector = new GestureDetector(myGestureListener);

}

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

if (null != mGestureDetector) {

return mGestureDetector.onTouchEvent(ev);

} else {

return super.onInterceptTouchEvent(ev);

}

}

@Override

public void flingToNext() {

if (null != mOnViewFlipperListener) {

int childCnt = getChildCount();

if (childCnt == 2) {

removeViewAt(1);

}

addView(mOnViewFlipperListener.getNextView(), 0);

if (0 != childCnt) {

setInAnimation(getContext(), R.anim.left_slip_in);

setOutAnimation(getContext(), R.anim.left_slip_out);

setDisplayedChild(0);

}

}

}

@Override

public void flingToPrevious() {

if (null != mOnViewFlipperListener) {

int childCnt = getChildCount();

if (childCnt == 2) {

removeViewAt(1);

}

addView(mOnViewFlipperListener.getPreviousView(), 0);

if (0 != childCnt) {

setInAnimation(getContext(), R.anim.right_slip_in);

setOutAnimation(getContext(), R.anim.right_slip_out);

setDisplayedChild(0);

}

}

}

public interface OnViewFlipperListener {

View getNextView();

View getPreviousView();

}

}


ViewFlipperDemoActivity.java

Java代碼

package com.tianjf;

import com.tianjf.MyViewFlipper.OnViewFlipperListener;

import android.app.Activity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.ScrollView;

import android.widget.TextView;

public class ViewFlipperDemoActivity extends Activity implements OnViewFlipperListener {

private MyViewFlipper myViewFlipper;

private int currentNumber;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

currentNumber = 1;

myViewFlipper = (MyViewFlipper) findViewById(R.id.myViewFlipper);

myViewFlipper.setOnViewFlipperListener(this);

myViewFlipper.addView(creatView(currentNumber));

}

@Override

public View getNextView() {

currentNumber = currentNumber == 10 ? 1 : currentNumber + 1;

return creatView(currentNumber);

}

@Override

public View getPreviousView() {

currentNumber = currentNumber == 1 ? 10 : currentNumber - 1;

return creatView(currentNumber);

}

private View creatView(int currentNumber) {

LayoutInflater layoutInflater = LayoutInflater.from(this);

ScrollView resultView = (ScrollView) layoutInflater.inflate(R.layout.flipper_view, null);

((TextView) resultView.findViewById(R.id.textView)).setText(currentNumber + "");

return resultView;

}

}


好了,代碼上完了,開始講解!

ViewFilpper的showPrevious()方法和showNext()方法是用來顯示已經在布局文件中定義好了的View,現在我們沒有在布局文件中為ViewFlipper添加View,那麼showPrevious()方法和showNext()方法就不能用了。但是我們怎麼實現滑動來切換View呢?用什麼方法呢?

這時候,我們就要自定義一個MyViewFlipper來監聽滑動事件,並做切換視圖的處理。

你可以讓MyViewFlipper實現OnTouchListener介面,然後實現onTouch方法,然後根據MotionEvent.ACTION_DOWN和MotionEvent.ACTION_UP的坐標判斷是不是滑動事件,就像ImageSwitcher中講解的那樣(htt p://bl og.csd n.n et/tianjf0514/article/details/7556487)

除了自己判斷是不是滑動事件,那麼Android有沒有直接提供哪個方法作為滑動事件的回調函數呢?答案是:提供了。OnGestureListener中的onFling方法就是滑動事件的回調函數。這時候你也許會毫不猶豫的讓MyViewFlipper實現OnGestureListener介面,並復寫onFling方法。這樣做當然可以,不過實現OnGestureListener介面不僅僅要復寫onFling方法,還要復寫其他的方法(onDown()、onShowPress()、onSingleTapUp()、onScroll()、onLongPress()),但是這些回調函數我們不需要,這就造成了垃圾代碼。

為了避免垃圾代碼,Android提供了一個類SimpleOnGestureListener已經實現了OnGestureListener介面和OnDoubleTapListener介面,並復寫了所有方法。那麼我們只要新建一個自己的MyGestureListener.java來繼承SimpleOnGestureListener,並有選擇性的復寫需要的方法(我們在此只復寫onFling方法)。

這時,我們就自定義了一個手勢類,並且這個手勢類會監聽滑動事件來做一些處理。但是我們怎麼利用這個手勢類呢?怎麼利用到MyViewFlipper類中去呢?

關於onFling方法,有一點要注意:不是每個View都能有onFling回調函數,一開始,我的flipper_view.xml布局文件最外層是一個LinearLayout,死活都走不到onFling方法,後來在外層又套了一個ScrollView,就能正常走到OnFling方法裡面了。

可以看到flingToNext方法和flingToPrevious方法裡面會判斷childCnt,如果為2,就removeViewAt(1);,然後再addView(mOnViewFlipperListener.getNextView(), 0);。這就要回顧一下ImageSwitcher的原理,ViewFlipper的原理和ImageSwitcher一樣,有且僅有2個子View,滑動時候就在這兩個子View上來回切換。index為0的就是當前看到的,index為1的就是看不見的。上面代碼的意思就是:當滑動時,必然要新添加一個View,那麼子View的個數有可能大於2,隨意要先判斷一下如果childCnt == 2,那麼就把index == 1的那個View(即看不見的View)給Remove調,然後把新添加的View添加到index == 0處。這樣可以減少內存消耗。

I. android自定義viewGroup中怎麼獲得子view,並測量到子view的寬高

findviewbyid就可以了,或者viewgroup。getview(index);等view繪制後就可以獲取到view的寬高了。

閱讀全文

與androidviewindex相關的資料

熱點內容
網盤忘記解壓碼怎麼辦 瀏覽:852
文件加密看不到裡面的內容 瀏覽:651
程序員腦子里都想什麼 瀏覽:430
oppp手機信任app在哪裡設置 瀏覽:185
java地址重定向 瀏覽:268
一年級下冊摘蘋果的演算法是怎樣的 瀏覽:448
程序員出軌電視劇 瀏覽:88
伺服器系統地址怎麼查 瀏覽:54
解壓游戲發行官 瀏覽:601
國外小伙解壓實驗 瀏覽:336
頂級大學開設加密貨幣 瀏覽:437
java重載與多態 瀏覽:528
騰訊應屆程序員 瀏覽:942
一鍵編譯程序 瀏覽:129
語音加密包哪個好 瀏覽:340
有什麼學習高中語文的app 瀏覽:282
安卓手機的表格里怎麼打勾 瀏覽:411
阿里雲伺服器有網路安全服務嗎 瀏覽:970
超解壓兔子視頻 瀏覽:24
單片機怎麼測負脈沖 瀏覽:174