大家好,我试图以编程方式管理 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);
}
非常感谢社区;)