【问题标题】:Android - UI XML to Java Code - Dynamic - LayoutParams Button with WeightAndroid - UI XML 到 Java 代码 - 动态 - 带权重的 LayoutParams 按钮
【发布时间】:2013-03-17 03:42:05
【问题描述】:

大家好,我想以编程方式管理 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);
}

非常感谢社区;)

【问题讨论】:

    标签: xml layout dynamic user-interface


    【解决方案1】:

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

    像这样:

    @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);
    
    
    
    
        }
    

    【讨论】:

    • 似乎它没有拉伸行以填满整个屏幕......有人可以帮助我吗?非常感谢!
    • 好吧,我的问题又得到了解决:P 使用这个线程:stackoverflow.com/questions/9683006/… 对不起,伙计们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 2021-10-08
    • 1970-01-01
    • 2020-11-28
    相关资源
    最近更新 更多