0

大家好,我试图以编程方式管理 UI,我将向表中添加一些行,但行数并不总是相同。所以我试图通过代码以编程方式执行此操作,因此它可以是动态的.

我已经尝试过了,只需要知道如何转换ImageButton和Button的layoutparams...两者必须相同,它们都是按钮..

非常感谢提前!!

<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"
    tools:context=".MainActivity" >



    <TableLayout 
            android:id="@+id/tableLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:stretchColumns="*"
             >


            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                >

                <ImageButton
                    android:id="@+id/imageButton1"
                    android:contentDescription="@string/removeIconDesc"
                    android:layout_width="0dip"
                    android:layout_weight="0.3"
                    android:layout_height="fill_parent"
                    android:src="@drawable/remove_icon" 
                    android:background="@null"/>



                <Button
                     android:layout_width="0dip"
                     android:layout_weight="1"
                     android:layout_height="fill_parent"
                     android:id="@+id/button1"
                     android:text="@string/button1" 
                     android:background="@drawable/pressed"
                     android:drawableRight="@drawable/arrow_left"
                     />

                <Button
                     android:layout_height="fill_parent"
                     android:layout_width="0dip"
                     android:layout_weight="1"
                     android:id="@+id/button2"
                     android:text="@string/button1" 
                     android:background="@drawable/pressed"
                     android:drawableLeft="@drawable/arrow_right"

                     />

                  <ImageButton
                    android:contentDescription="@string/removeIconDesc"
                    android:id="@+id/imageButton2"
                    android:layout_width="0dip"
                    android:layout_weight="0.3"
                    android:layout_height="fill_parent"
                    android:src="@drawable/remove_icon" 
                    android:background="@null"/>
            </TableRow>
    </TableLayout>

我正在尝试将在 XML 中定义的这个 UI 转换为 java 代码,因为我需要动态地执行它,到目前为止我最好的尝试是下面的这个:但是现在我不能继续前进.. 因为我无法设置权重我的 ImageButton 和 Buttons 的 LayoutParams...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    tLayout = new TableLayout(this);
    tLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    tLayout.setStretchAllColumns(true);

    TableRow tbRow = new TableRow(mainActivity);

    TableRow.LayoutParams tL = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    tL.weight = 1;

    tbRow.setLayoutParams(tL);

    RelativeLayout.LayoutParams iM = new RelativeLayout.LayoutParams(0, LayoutParams.MATCH_PARENT);   //this is the right LayoutParams for the ImageButton... I guess
    iM.weight = 0 -- This is not possible.... Error On IDE


    //still need to do the translation to the two buttons... and the other ImageButton, but knowing how to do for the ImageButton I guess I can do the rest..


    setContentView(tLayout);
}

非常感谢社区;)

4

1 回答 1

0

我修复了它,删除了拉伸表格布局列的行,并删除了我将 imagebutton 的背景设置为空的方法......

像这样:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        tLayout = new TableLayout(this);
        tLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


        TableRow tbRow = new TableRow(this);

        TableRow.LayoutParams tL = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        tL.weight = 1;

        TableRow.LayoutParams iM = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT);   //this if for the ImageButton..
        iM.weight = (float) 0.3;


        TableRow.LayoutParams iMB = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT);   //this if for the ImageButton..
        iMB.weight = (float) 1;


        ImageButton m = new ImageButton(this);
        m.setImageResource(R.drawable.remove_icon);

        m.setLayoutParams(iM);

        Button b = new Button(this);

        b.setText("Cenas");
        b.setLayoutParams(iMB);
        b.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.arrow_left,0);

        Button b2 = new Button(this);
        b2.setBackgroundResource(R.drawable.pressed);
        b2.setText("Conas");
        b2.setLayoutParams(iMB);
        b2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.arrow_right,0,0,0);

        ImageButton m2 = new ImageButton(this);
        m2.setImageResource(R.drawable.remove_icon);
        m2.setLayoutParams(iM);

        tbRow.addView(m);
        tbRow.addView(b);
        tbRow.addView(b2);
        tbRow.addView(m2);

        tLayout.addView(tbRow, tL);


        setContentView(tLayout);




    }
于 2013-03-16T19:42:05.327 回答