2

一定量のパディングがある線形レイアウトがあります。レイアウトの外に出ることなく、その線形レイアウト内の任意の場所に画像ボタンをランダムに配置したい。ここに私のxmlコードがあります:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="45dp"
    android:text="Text 1"
    android:id="@+id/text1" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="30dp"
    android:text="Text 2"
    android:id="@+id/text2" />
<LinearLayout
    android:id="@+id/lay1"
    android:layout_width="match_parent"
    android:layout_height="450dp"
    android:layout_margin="20dp">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:src="@drawable/ib"
    android:id="@+id/button1"/>

</LinearLayout>
</LinearLayout>

ImageButtonbutton1を layout 内にランダムに配置したいlay1

レイアウトの幅と高さを取得してランダム関数に入力しようとしましたが、これを使用して画像ボタンに左マージンと上マージンを提供しましたが、それを行うたびにアプリがクラッシュし続けます。Javaコードは次のとおりです。

ImageButton b = (ImageButton) findViewById(R.id.button1);
LinearLayout l = (LinearLayout) findViewById(R.id.lay1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) b.getLayoutParams();
int width = l.getWidth();
int height= l.getHeight();
params.leftMargin = new Random().nextInt(width);
params.topMargin = new Random().nextInt(height);
b.setLayoutParams(params);

上記の方法は機能せず、このアクティビティを開くとアプリが常にクラッシュします。誰かが私を助けてくれますか?

4

3 に答える 3

0

部分的な解決策があります。次のコードは、ボタンをランダムな場所に配置しますが、画面外に置くことができます。ランダムジェネレーターをいじることで、これを防ぐことができるはずです。

public class MainActivity extends ActionBarActivity {

int width, height;
LinearLayout linearLayout;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    linearLayout = (LinearLayout) findViewById(R.id.ll);
    linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Log.v(getClass().getSimpleName(), "onGlobalLayout");
            width = linearLayout.getWidth();
            height = linearLayout.getHeight();

            placeButton();

            if (Build.VERSION.SDK_INT >= 16) {
                linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        }
    });

    button = (Button) findViewById(R.id.button);
}

private void placeButton() {
    Log.v(getClass().getSimpleName(), "width: " + width + " Height: " + height);
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();

    Random rand = new Random();

    params.leftMargin = rand.nextInt(width);
    params.topMargin = rand.nextInt(height);

    Log.v(getClass().getSimpleName(), "left: " + params.leftMargin + " top: " + params.topMargin);
    button.setLayoutParams(params);
    button.invalidate();
}

}

linearlayout が描画されるのを待つだけです。次に、幅と高さを取得し、ボタンをランダムな場所に配置します。

注: これにより、画面を回転させるとボタンが移動します。OnGlobalLayoutListenerifを追加するだけでこれを防ぐことができますsavedInstanceState == null

于 2015-02-10T13:48:57.813 に答える
0

コードをこれに置き換えるだけです

ImageButton b = (ImageButton) findViewById(R.id.button1);
LinearLayout l = (LinearLayout) findViewById(R.id.lay1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)l.getLayoutParams();
int width = l.getWidth();
int height= l.getHeight();
params.leftMargin = new Random().nextInt(width);
params.topMargin = new Random().nextInt(height);
l.setLayoutParams(params);
于 2015-02-10T14:07:30.720 に答える