
<?xml version="1.0" enCoding="utf-8"?><androID.support.constraint.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/main" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".MainActivity"> <button androID:ID="@+ID/btn" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginStart="0dp" androID:layout_margintop="0dp" androID:layout_marginEnd="0dp" androID:layout_marginBottom="0dp" androID:minWIDth="0dp" androID:minHeight="0dp" androID:text="@string/start" androID:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_totopOf="parent" /></androID.support.constraint.ConstraintLayout>
我的简化代码:
import androID.annotation.Suppresslint;import androID.os.Handler;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.util.displayMetrics;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.Widget.button;import java.util.Random;public class MainActivity extends AppCompatActivity { final Random rand = new Random(); private button btn; private float wIDthPixels,heightPixels; private float minBtnSize,maxBtnSize; @Suppresslint("ClickableVIEwAccessibility") @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); /* Obtaining screen's display propertIEs. */ final displayMetrics displaymetrics = new displayMetrics(); getwindowManager().getDefaultdisplay().getMetrics(displaymetrics); wIDthPixels = displaymetrics.wIDthPixels; heightPixels = displaymetrics.heightPixels; // includes BotNavbar heightPixels -= 162; // size of the "Virtual Nav bar" at the bottom float minDim = Math.min(wIDthPixels,heightPixels); minBtnSize = minDim/25; maxBtnSize = minDim/2; /* Setting up the experiment's button*/ btn = findVIEwByID(R.ID.btn); btn.setontouchListener(new VIEw.OntouchListener(){ public boolean ontouch(VIEw vIEw,MotionEvent event){ /* touch Events. */ switch (event.getAction()) { case MotionEvent.ACTION_UP: /* Move and resize the button. */ launchNewAttempt(x,y); break; default: break; } return true; } }); /* Setting up the beginning button's state. */ init(); } /** * Initializing the button shown at the very beginning. */ private voID init() { btn.setX(0); btn.setY(0); btn.setWIDth((int)wIDthPixels); btn.setHeight((int)heightPixels); } /** * Called whenever the user releases the experiment's button (pressed "UP"). */ private voID launchNewAttempt() { /* Random size for the "new" button. */ int size=(int)(minBtnSize+(maxBtnSize-minBtnSize)*rand.nextDouble()); btn.setWIDth(size); btn.setHeight(size); /* Random position for the "new" button. */ float dx = rand.nextfloat() * (wIDthPixels - size); float dy = rand.nextfloat() * (heightPixels - size); btn.setX(dx); btn.setY(dy); }} 预期行为:
填充整个屏幕的按钮出现在开头.按下后,相同的按钮被调整大小(正方形,边长至少为屏幕宽度的1/25,最多为屏幕宽度的1/2),并在屏幕上随机移动.每次用户再次按下该按钮时,其行为方式相同(新的随机位置和大小).
我的问题:
btn.setX()和btn.setY()似乎没有按预期运行.我原以为他们会在屏幕上确定按钮的绝对位置,但是对于init()使用以下示例代码有一个奇怪的行为:
btn.setX(0);btn.setY(0);btn.setWIDth(150);btn.setHeight(150);
大小似乎没问题,但位置似乎是相对于屏幕中间的(虽然它似乎也取决于按钮的大小,因为使用前面介绍的代码它正确填充整个屏幕).我希望这个位置相对于屏幕的左上角(不包括顶部的Actionbar / Titlebar,也不包括底部的Navigationbar).
当我运行当前代码时,按钮实际上有时会在屏幕外关闭.这有什么不对?
解决方法 我尝试用linearLayout替换布局的根节点代码并且它正常工作,这意味着问题与ContraintLayout的内部逻辑有关.知道我试图改变按钮的位置,考虑约束而不是绝对位置,特别是关于按钮的顶部和起始边距.因此,launchNewAttempt()方法可以类似于:private voID launchNewAttempt() { final int size = (int) (minBtnSize + (maxBtnSize - minBtnSize) * rand.nextDouble()); final int left = (int) (rand.nextfloat() * (wIDthPixels - size)); final int top = (int) (rand.nextfloat() * (heightPixels - size)); final ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btn.getLayoutParams(); layoutParams.wIDth = size; layoutParams.height = size; layoutParams.leftmargin = left; layoutParams.topmargin = top; btn.setLayoutParams(layoutParams);} 我还只保留了两个约束,将按钮放在布局中,并删除了底部和底部的按钮:
<button androID:ID="@+ID/btn" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginStart="0dp" androID:layout_margintop="0dp" androID:layout_marginEnd="0dp" androID:layout_marginBottom="0dp" androID:minWIDth="0dp" androID:minHeight="0dp" androID:text="Start" androID:visibility="visible" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_totopOf="parent" />
希望这对你有用.
总结以上是内存溢出为你收集整理的android – 按钮的随机大小和位置:意外行为全部内容,希望文章能够帮你解决android – 按钮的随机大小和位置:意外行为所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)