【问题标题】:How to change "layout_margintop"?如何更改“layout_margintop”?
【发布时间】:2015-06-10 20:21:48
【问题描述】:

我按照按钮的方式做了一些事情,它生成随机数以与导致屏幕上按钮位移的坐标互换,但我不知道如何替换生成的平面 X 和 Y 的数量,请帮忙,这些是我的应用程序代码,只是我教书,从未找到足够的知识支持。

由 Google 翻译自波兰语

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

        }

    }
}

【问题讨论】:

    标签: java android eclipse android-layout


    【解决方案1】:

    您需要将点击事件添加到您的 XML。

    android:onclick="onClick"
    

    在按钮标签中

    【讨论】:

    • 现在我有错误:在包 'android'activity_main.xml /Reflex!/res/layout 第 11 行 Android AAPT 问题中找不到属性 'onclick' 的资源标识符
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2022-07-24
    • 1970-01-01
    相关资源
    最近更新 更多