android 固定底部 布局_如何让Android的布局的底部?

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);

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());

this is the XML

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">

and the View im trying to add:

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();

}

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

解决方案

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);

All this calculating with the view.getHeight() is not good. Doing it like this works without you having to manage any of that yourself.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值