0

我正在尝试将动态网格布局添加到具有文本视图的父线性布局。

这是代码。

public class ActivityMain extends Activity {

LinearLayout.LayoutParams mLayoutParams;
GridLayout.LayoutParams mGridLayoutParams;
Button[] button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_main);

    ViewGroup parent = (ViewGroup) findViewById(R.id.ll);

    mLayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    GridLayout gl = new GridLayout(this);
    gl.setLayoutParams(mLayoutParams);
    gl.setOrientation(GridLayout.HORIZONTAL);
    gl.setColumnCount(8);
    gl.setRowCount(1);
    button = new Button[8];
    mGridLayoutParams = new GridLayout.LayoutParams();
    mGridLayoutParams.height=android.widget.GridLayout.LayoutParams.WRAP_CONTENT;

    for(int i=0;i<8;i++)
    {
        button[i] = new Button(this);
        button[i].setLayoutParams(mGridLayoutParams);
        button[i].setText(String.valueOf(i));

        gl.addView(button[i]);
    }
    parent.addView(gl);
}

}

这是我使用的布局

<LinearLayout 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:orientation="vertical" 
android:gravity="center"
android:id="@+id/ll"
tools:context=".ActivityMain" >

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:gravity="center"
    android:text="New Product Form"
    android:textSize="20sp"
    android:typeface="serif" 
    android:layout_weight="1"/>
</LinearLayout>

因为我的声誉低于 10,所以我无法从我的设备上传图像。请找到我在此链接获得的输出 DEVICE IMAGE

谁能解释我在做什么错。提前致谢。

4

1 回答 1

1

间谍。每个按钮都应该有一个新的 GridLayout.LayoutParams。希望得到帮助:https ://stackoverflow.com/a/31836423/898431

于 2015-08-05T15:24:44.193 回答