我在Android中使用相對佈局,並且在向屏幕底部添加視圖時,視圖的放置位置低於屏幕的實際底部,用戶無法看到它。如何在android中獲取佈局的底部?
view.setY(container.getBottom()-view.getHeight());
container.addView(view);
container是一個RelativeLayout。我也嘗試使用container.getHeight()給了我相同的結果,我發現使用xml文件的所有其他解決方案,這不適用於我,因爲我需要動態地添加視圖上的隨機位置屏幕下方,這意味着
view.setY(container.getHeight()-Common.random.nextFloat()*100-view.getHieght());
這是XML
<RelativeLayout xmlns: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"
android:id="@+id/layout"
tools:context="il.co.ovalley.dashvsponypolice.app.MainActivity">
</RelativeLayout>
和查看IM嘗試添加:
public class GameView extends ImageView{
public GameView(RelativeLayout container) {
super(container.getContext());
m_container=container;
container.addView(this);
}
}
public class Cop extends GameView {
public Cop(RelativeLayout container) {
super(container);
m_isShooting=false;
m_LoadingTime=10;
m_isLoading=false;
m_xSpeed=1.5f;
setY(container.getHeight()-Common.random.nextFloat()*100-getHeight()/*-getPaddingBottom()*/);
float x=(Common.random.nextFloat()*m_container.getWidth());
setX(x);
drwableState=0;
changeDirection();
}
我已經測試了不同版本的android和屏幕尺寸並獲得相同的結果。到目前爲止,我發現的唯一解決方案是從容器的高度減去任意一個int,在一個設備上似乎可以,並且希望爲其他設備提供最好的結果,我相信有更好的解決方案。
日Thnx
您的相對佈局是否有固定高度? – Libin
nope。 match_parent,並且是根視圖 –
根據Xaver答案,嘗試將視圖設置爲父測試 – Libin