Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式设置按钮的布局(计算器)_Java_Android_Android Layout - Fatal编程技术网

Java 以编程方式设置按钮的布局(计算器)

Java 以编程方式设置按钮的布局(计算器),java,android,android-layout,Java,Android,Android Layout,我试图制作一个动态构建的简单计算器,而不是使用拖放方法手动创建按钮,然后在XML文件中设置它们的id 动态地(如中),使用循环将按钮自动放置在一组行上 我所尝试的: 我已经试过了RelativeLayout和LinearLayout 手动设置每个按钮的X和Y值btn[i]。setX250*i;但我怀疑,当在各种各样的设备上使用时,这将是一个问题 My activity_main.xml代码: 相对性是我想要按钮的位置 虽然乍一看,这可能与这里的其他问题类似,但请继续阅读 非常感谢 您可以使用Gr

我试图制作一个动态构建的简单计算器,而不是使用拖放方法手动创建按钮,然后在XML文件中设置它们的id

动态地(如中),使用循环将按钮自动放置在一组行上

我所尝试的: 我已经试过了RelativeLayout和LinearLayout

手动设置每个按钮的X和Y值btn[i]。setX250*i;但我怀疑,当在各种各样的设备上使用时,这将是一个问题

My activity_main.xml代码: 相对性是我想要按钮的位置

虽然乍一看,这可能与这里的其他问题类似,但请继续阅读

非常感谢

您可以使用GridLayout而不是RelativeLayout


以编程方式添加按钮时,它将附加到子按钮。不在乎位置。

参考此链接:@MayurPanchal,您所参考的两个链接都使用拖放方法。我假设你没有完全阅读我的问题。
<?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/parentView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/answerSheet"
        android:layout_width="397dp"
        android:layout_height="146dp"
        android:autofillHints=""
        android:background="@layout/rounded_border_edittext"
        android:ems="10"
        android:inputType="number"
        android:paddingTop="100dp"
        android:textColorLink="@android:color/background_dark"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.027" />

    <RelativeLayout
        android:id="@+id/relativelay"
        android:layout_width="395dp"
        android:layout_height="553dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/answerSheet">

    </RelativeLayout>


</android.support.constraint.ConstraintLayout>
package com.example.calc;

import android.app.ActionBar;
import android.graphics.drawable.Drawable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText answer_sheet = findViewById(R.id.answerSheet);
        answer_sheet.setText("test");
        answer_sheet.setFocusable(false); // you won't be able to click and edit.
        answer_sheet.setCursorVisible(false); // this way you won't be able to see "copy, paste, cut" etc.


        RelativeLayout r_layout = findViewById(R.id.relativelay);
        Button btn[] = new Button[10];

        for(int i=0; i<10; i++){
            btn[i] = new Button(this);
            Log.d("test","btn: " + btn[i]);
            r_layout.addView(btn[i]);
            btn[i].setText(Integer.toString(i));
            btn[i].setX(250 * i);
            btn[i].setY(250* i);
        }
    }

}

-> ConstraintLayout (id: parentView)

      -> Textview (id: answerSheet)

      -> RelativeLayout (id: relativelay)
<GridLayout 
    ...
    android:id="@+id/GridLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:rowCount="4"
    android:orientation="horizontal"
    ...
>