如何让Android的布局的底部? [英] how to get the bottom of a layout in android?
问题描述
我在Android中使用相对布局,并添加视图到屏幕底部视图时比屏幕的实际底部,用户倾斜actualy看到它放在低。
I'm using a relative layout in Android, and when adding a view to the bottom of the screen the view is placed lower than the actual bottom of the screen, where the user cant actualy see it.
view.setY(container.getBottom()-view.getHeight());
container.addView(view);
容器是一个RelativeLayout的。我也尝试过用container.getHeight(),它给了我同样的结果,和所有其他的解决方案,我发现在这里使用的XML文件,它不工作对我来说,因为我需要动态地添加视图上面的随机位置屏幕下方,这意味着
container is a RelativeLayout. i have also tried to use container.getHeight() which gave me the same result, and all the other solutions i found where using the xml file, which doesnt work for me since i need to add the view dynamically in a random position above the bottom of the screen, meaning
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唯一的解决办法,我敢肯定有一个更好的解决方案。
I have tested that on different versions of android and screen sizes and got the same result. so far, the only solution I found was subtracting an arbitrary int from the container's height which seems ok on one device and hope for the best for the others, I'm sure there is a better solution.
结果日Thnx
推荐答案
为什么你不只是把屏幕底部的观点与 ALIGN_PARENT_BOTTOM
,然后申请一个随机填充?
Why don't you just place the view at the bottom of the screen with ALIGN_PARENT_BOTTOM
and then apply a random padding?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
view.setPadding(0, 0, 0, randomValue); // left, top, right, bottom
container.addView(view, params);
这一切与计算 view.getHeight()
是不好的。做这样的工作,您无需任何管理那你自己。
All this calculating with the view.getHeight()
is not good. Doing it like this works without you having to manage any of that yourself.
这篇关于如何让Android的布局的底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!