我正在处理一个Android项目,需要在运行时将按钮添加到Layout中。因此,这是布局xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/layout">

</LinearLayout>


这是活动:

import android.app.Activity;
import android.widget.*;
import android.widget.TableLayout.LayoutParams;
import android.os.Bundle;

public class PluginsActivity extends Activity{

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plugins);

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        Button butt = new Button(this);
        Button butt2 = new Button(this);
        butt.setText("lol");
        butt2.setText("lol2");
        butt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        butt2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        layout.addView(butt);
        layout.addView(butt2);

    }

}


现在的问题是,当我开始活动时,按钮1“覆盖”按钮2。实际上,如果我使用LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT),两个按钮都会显示。有没有一种方法可以在LinearLayout上添加“换行”?屏幕应该是这样的:



| ******* Button1 ********** ||



| ******* Button2 ********** ||



谢谢。

最佳答案

在布局文件中使LinearLayout的方向垂直

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


butt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
butt2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

10-07 19:14
查看更多