如何改变" layout_margintop"?

时间:2015-06-10 20:21:48

标签: java android eclipse android-layout

我沿着一个按钮的行做了一些东西,它产生的随机数与屏幕上按钮位移的坐标互换,但我不知道如何替换生成的X和Y平面的数量,请帮助,这些是我申请的代码,只是我教,从未找到足够的支持我的知识状态。

由波兰语翻译

   <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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.reflex.MainActivity" >

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="45dp"
        android:text="Reflex!" />

</RelativeLayout>


    package com.example.reflex;





    import java.util.Random;
    import android.support.v7.app.ActionBarActivity;
    import android.annotation.SuppressLint;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;


    public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener

    {

    private Button generuj;
    private int wynik1;
    private int wynik2;
    private final static int min = 1;
    private final static int maxh = 109;
    private final static int maxv = 239;
    private Random randomGenerator;

    @Override
    protected void onCreate(Bundle savedInstanceState) 

    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        generuj = (Button) findViewById(R.id.b1);
        randomGenerator = new Random();




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 

    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 

    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) 

    {
            return true;
    }
        return super.onOptionsItemSelected(item);
    }


    @SuppressLint("NewApi") @Override
    public void onClick(View v) 
    {
        if(v.getId() == R.id.b1)
        {
            int temp = randomGenerator.nextInt(maxh) + min;
            int temp1 = randomGenerator.nextInt(maxv) + min;
            generuj.setX(temp1);
            generuj.setY(temp);

        }

    }
}

1 个答案:

答案 0 :(得分:0)

您需要将on click事件添加到XML中。

android:onclick="onClick"

在按钮标签