我使用了以下方法,但没有成功
Integer [] location = new Integer[];
view.getLocationOnScreen(location);
getLocationInWindow(int[]) MyCode: int[] locatoins = new int[2];
rulerLyt.findViewById(2).getLocationInWindow(locatoins);
x = locatoins[0];
float y = 0f;
y = locatoins[1];
imgBaby.setTranslationX(x);
imgBaby.setTranslationY(y);
请给出您的评论/解决方案以实现这一目标
答案 0 :(得分:1)
这些是View类的易于访问的方法。
int xCordinate = rulerLyt.findViewById(2).getX();
int yCordinate = rulerLyt.findViewById(2).getY();
答案 1 :(得分:0)
如果在创建视图后立即调用提供的代码,它将返回0。因为视图膨胀和测量是异步的。它们发生在/*this is the .ts file*/
constructor( public af: AngularFire, public db: AngularFireDatabase) {
this.items = db.list('items').valueChanges();
db.list('/sentences').valueChanges().subscribe(sentences => {
this.sentences = sentences;
console.log(this.sentences);
});
}
的不同迭代上。您需要使用<!--this in the html-->
<div>
<ul class="sent">
<li *ngFor="let sentence of sentences"
[class.selected]="sentence === selectedsentence"
(click)="onSelect(sentence)">{{sentence}}
<!-- this is displaying all the items in the firebase; i need only one at a time-->
</li>
</ul>
</div>
作为变体。
MainThread
ViewTreeObserver
int[] locatoins = new int[2];
rulerLyt.findViewById(2).getLocationInWindow(locatoins);
float x = locatoins[0];
float y = locatoins[1];
imgBaby.setTranslationX(x);
imgBaby.setTranslationY(y);