導航:首頁 > 操作系統 > android獲得控制項位置

android獲得控制項位置

發布時間:2022-09-15 03:44:20

『壹』 怎麼獲取控制項在屏幕上的位置android

getLocationOnScreen ,計算該視圖在全局坐標系中的x,y值,(注意這個值是要從屏幕頂端算起,也就是索包括了通知欄的高度)//獲取在當前屏幕內的絕對坐標

getLocationInWindow ,計算該視圖在它所在的widnow的坐標x,y值,//獲取在整個窗口內的絕對坐標 (不是很理解= =、)

getLeft , getTop, getBottom, getRight, 這一組是獲取相對在它父親里的坐標

如果在Activity的OnCreate()事件輸出那些參數,是全為0,要等UI控制項都載入完了才能獲取到這些。
import android.widget.ImageView;

public class LocationActivity extends Activity {
/** Called when the activity is first created. */
private ImageView t = null;
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

t = (ImageView)findViewById(R.id.l);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new buttonListener());
}
public class buttonListener implements OnClickListener{

public void onClick(View v)
{
int[] location = new int[2];
t.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
System.out.println("x:"+x+"y:"+y);
System.out.println("圖片各個角Left:"+t.getLeft()+"Right:"+t.getRight()+"Top:"+t.getTop()+"Bottom:"+t.getBottom());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas、android、com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button"/>
<ImageView
android:id="@+id/l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/a" />
</LinearLayout>

『貳』 android中獲得控制項的位置(相對於布局文件)

正確的代碼如下所示

java">TextViewtv=(TextView)findViewById(R.id.text);
tv.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener(){
@Override
publicvoidonGlobalLayout(){
//這里執行獲取位置大小操作
intleft=tv.getLeft();
inttop=tv.getTop();
intbottom=tv.getBottom();
intright=tv.getRight();
//tv相對於父布局的坐標就得出來了

}
}

view的位置和大小是通過onLayout和onMeasure方法計算出來的,執行到activity的onCreate()方法時,尚未開始計算控制項的大小和位置,所以是取不到的

OnGlobalLayoutListener 是ViewTreeObserver的內部類,當一個視圖樹的布局發生改變時,可以被ViewTreeObserver監聽到,這是一個注冊監聽視圖樹的觀察者(observer),在視圖樹的全局事件改變時得到通知

除了OnGlobalLayoutListener ,ViewTreeObserver還有如下內部類:


interfaceViewTreeObserver.OnGlobalFocusChangeListener

當在一個視圖樹中的焦點狀態發生改變時,所要調用的回調函數的介面類


interfaceViewTreeObserver.OnGlobalLayoutListener

當在一個視圖樹中全局布局發生改變或者視圖樹中的某個視圖的可視狀態發生改變時,所要調用的回調函數的介面類


interfaceViewTreeObserver.OnPreDrawListener

當一個視圖樹將要繪制時,所要調用的回調函數的介面類


interfaceViewTreeObserver.OnScrollChangedListener

當一個視圖樹中的一些組件發生滾動時,所要調用的回調函數的介面類


interfaceViewTreeObserver.OnTouchModeChangeListener

當一個視圖樹的觸摸模式發生改變時,所要調用的回調函數的介面類

『叄』 Android studio 中怎樣實現控制項相對位置定位

ConstraintLayout約束布局了解一下

『肆』 android動態添加控制項,怎樣指定位置

首先你得定義一個 LayoutParams:
RelativeLayout.LayoutParams s = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
s.addRule(RelativeLayout.CENTER_IN_PARENT, -1);
//添加位置信息 -1表示相對於父控制項的位置 ,如果要相對某個平級控制項則參數是該控制項的ID

s.setMargins(10, 10, 10, 10);//設置左,上,右,下,的距離

上面的定義好了之後可以用了:

imgApple2.setLayoutParams(s);
insertLayout.addView(imgApple2,100,100);

『伍』 android 怎麼通過代碼設置控制項位置

你可以通過findViewById()先獲得這個控制項剩下的就是你去調用這個控制項的方法在你的代碼裡面設置控制項的寬高了。

『陸』 Android開發中在view中怎麼指定控制項的位置

用LayoutParams: RelativeLayout insertLayout = (RelativeLayout)view1.findViewById(R.id.screen);//screen是一個RelativeLayout 布局的id ImageView imgApple2 = new ImageView(MainActivity.this); imgApple2.setBackgroundColor(Color.pars...

『柒』 android怎樣動態生成布局文件,指定控制項顯示位置

控制項位置發生改變時
用sharedpreferences記錄控制項位置
第二次進入時讀取記錄了的內容,動態生成布局

『捌』 android 怎麼在布局裡面獲取控制項

layout為布局,布局裡面可以放任何空間,獲取空間可以用findViewById方法獲取

android 獲取某個布局控制項 添加到另一個布局中

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.layout456);
ImageView imgApple2 = new ImageView(this);
imgApple2.setImageResource(R.drawable.ic_launcher);
relativeLayout.addView(imgApple2);

LayoutInflater factorys = LayoutInflater.from(MainActivity.this);
final View textEntryView = factorys.inflate(R.layout.layout1, null);

// LinearLayout linearLayout = (LinearLayout) textEntryView
// .findViewById(R.id.layout1);
// relativeLayout.addView(linearLayout);
EditText editText1 = (EditText) textEntryView
.findViewById(R.id.editText1);

relativeLayout.addView(editText1);

『玖』 android listview 控制項在什麼位置

android listview,在eclipse或者android studio在代碼編輯窗口,輸入listview,ctrl+滑鼠左鍵點擊,如果導入了源代碼,就可以查看具體的代碼位置,主要是用來展示一條條的數據的。

『拾』 android中 4.0版本 控制項直接getX()getY()就可以獲取到位置,2.1中有什麼方法可以獲

如果是get的話,android2.2有getLeft() getRight() getBottom() getTop() ,

getLeft()和getBottom()就對應你的GetX和GetY

但Andriod2.2卻沒有對應的set,如果要set只能使用setLayoutParams

閱讀全文

與android獲得控制項位置相關的資料

熱點內容
如何在文件夾中顯示頁碼 瀏覽:354
雲伺服器登不上qq 瀏覽:417
程序員四級工程師 瀏覽:715
薄荷app怎麼把體重清零 瀏覽:644
草料二維碼加密怎麼製作 瀏覽:851
04s519隔油池圖集pdf 瀏覽:242
程序員搞測試 瀏覽:552
蘋果app應用隱藏了怎麼辦 瀏覽:660
PDF調取 瀏覽:199
獨立柱加密需要什麼條件 瀏覽:814
php培訓出來找不到工作 瀏覽:106
小程序克隆源碼 瀏覽:448
python整數整除負數 瀏覽:880
遮天用什麼小說app看 瀏覽:645
什麼可以發類似朋友圈的app 瀏覽:495
cmd查找命令行 瀏覽:661
如何申請域名需要虛擬伺服器 瀏覽:497
氣體流量的演算法 瀏覽:634
大族加密狗滑鼠 瀏覽:23
php資料庫登錄界面 瀏覽:657