Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.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
Android 按钮的排列_Android - Fatal编程技术网

Android 按钮的排列

Android 按钮的排列,android,Android,我创建了一系列按钮,但所有按钮都是按垂直顺序排列的。 我希望一行有3个按钮,下一行有3个按钮,依此类推 这是我的代码,请检查它应该做的地方 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout layout = (LinearLayout)findViewById(R.

我创建了一系列按钮,但所有按钮都是按垂直顺序排列的。
我希望一行有3个按钮,下一行有3个按钮,依此类推

这是我的代码,请检查它应该做的地方

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LinearLayout layout = (LinearLayout)findViewById(R.id.liLayout);
    for (int i = 1; i < 10; i++)
    {

        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(

                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        Button b = new Button(this);
        b.setText("" + i);
        b.setId(100 + i);
        b.setWidth(50);
        b.setHeight(20);
        layout.addView(b, p);

    }
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout布局=(LinearLayout)findViewById(R.id.liLayout);
对于(int i=1;i<10;i++)
{
LinearLayout.LayoutParams p=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_内容,
LinearLayout.LayoutParams.WRAP_内容
);
按钮b=新按钮(此按钮);
b、 setText(“+i”);
b、 setId(100+i);
b、 设定宽度(50);
b、 设置高度(20);
布局。添加视图(b,p);
}
}
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//应该有一个垂直方向
LinearLayout布局=(LinearLayout)findViewById(R.id.liLayout);
LinearLayout.LayoutParams p=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_内容,
LinearLayout.LayoutParams.WRAP_内容
);

LinearLayout rowLayout=null;***复制、粘贴、欣赏

<RelativeLayout android:layout_width="wrap_content"
              android:layout_height="wrap_content">

<Button android:id="@+id/button1" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:text="Button1"/>

<Button android:id="@+id/button2"
        android:layout_height="wrap_content"  
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/button1" 
        android:layout_alignTop="@+id/button1" 
        android:layout_alignBottom="@+id/button1" 
        android:text="Button2"/>

<Button android:id="@+id/button3"
        android:layout_height="wrap_content"  
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/button2" android:layout_alignTop="@+id/button2" android:layout_alignBottom="@+id/button2" 
        android:text="Button3"/>

<Button android:id="@+id/button4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"  
        android:layout_below="@+id/button1"  
        android:layout_alignLeft="@+id/button1" 
        android:layout_alignRight="@+id/button1"
        android:text="Button4"/>

<Button android:id="@+id/button5" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" 
        android:layout_below="@+id/button2"
        android:layout_alignLeft="@+id/button2" 
        android:layout_alignRight="@+id/button2"  
        android:text="Button5"/>

<Button android:id="@+id/button6"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"  
        android:layout_alignLeft="@+id/button3" android:layout_alignRight="@+id/button3"
        android:layout_below="@+id/button3"  
        android:text="Button6"/>

<Button android:id="@+id/button7"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" 
        android:layout_alignLeft="@+id/button4" 
        android:layout_alignRight="@+id/button4" 
        android:layout_below="@+id/button4"  
        android:text="Button7"/>

<Button android:id="@+id/button8"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"  
        android:layout_below="@+id/button5"
        android:layout_alignLeft="@+id/button5" 
        android:layout_alignRight="@+id/button5"  
        android:text="Button8"/>


<Button android:id="@+id/button9"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"  
        android:layout_below="@+id/button6" android:layout_alignLeft="@+id/button6" android:layout_alignRight="@+id/button6"
        android:text="Button9"/>


阅读以下内容:

hi stephane..rowLayout=new LinearLayout();语句给出了一个编译错误..“构造函数LinearLayout未定义”,当我给出rowLayout=new LinearLayout(null)时,它的工作方式是..但给出了运行时错误Hey stephane..我应该如何使按钮水平填充屏幕(并且大小相等),因为在所有按钮中我都有文本框..那么我如何整合所有这些,使它们填满屏幕呢horizontally@Anshuman第一件事是接受我的回答。:)第二件事是给所有3个按钮加一个权重b.设置权重(1)在它们的父视图中。假设我必须创建100个按钮。那么我应该做什么呢。简单地添加100个按钮,就像这样。想想如果我需要1000个按钮。嗯……我会在网格视图中填充它们。对于所有1000个按钮。这对开发人员来说是没有效率的
<RelativeLayout android:layout_width="wrap_content"
              android:layout_height="wrap_content">

<Button android:id="@+id/button1" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:text="Button1"/>

<Button android:id="@+id/button2"
        android:layout_height="wrap_content"  
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/button1" 
        android:layout_alignTop="@+id/button1" 
        android:layout_alignBottom="@+id/button1" 
        android:text="Button2"/>

<Button android:id="@+id/button3"
        android:layout_height="wrap_content"  
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/button2" android:layout_alignTop="@+id/button2" android:layout_alignBottom="@+id/button2" 
        android:text="Button3"/>

<Button android:id="@+id/button4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"  
        android:layout_below="@+id/button1"  
        android:layout_alignLeft="@+id/button1" 
        android:layout_alignRight="@+id/button1"
        android:text="Button4"/>

<Button android:id="@+id/button5" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" 
        android:layout_below="@+id/button2"
        android:layout_alignLeft="@+id/button2" 
        android:layout_alignRight="@+id/button2"  
        android:text="Button5"/>

<Button android:id="@+id/button6"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"  
        android:layout_alignLeft="@+id/button3" android:layout_alignRight="@+id/button3"
        android:layout_below="@+id/button3"  
        android:text="Button6"/>

<Button android:id="@+id/button7"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" 
        android:layout_alignLeft="@+id/button4" 
        android:layout_alignRight="@+id/button4" 
        android:layout_below="@+id/button4"  
        android:text="Button7"/>

<Button android:id="@+id/button8"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"  
        android:layout_below="@+id/button5"
        android:layout_alignLeft="@+id/button5" 
        android:layout_alignRight="@+id/button5"  
        android:text="Button8"/>


<Button android:id="@+id/button9"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"  
        android:layout_below="@+id/button6" android:layout_alignLeft="@+id/button6" android:layout_alignRight="@+id/button6"
        android:text="Button9"/>