1

これが私のコードです:

 //define tablerows. each table row has max 3 buttons
 LinearLayout llRow1 = (LinearLayout)findViewById(R.id.llRow1); llRow1.removeAllViews();

   float scale = getResources().getDisplayMetrics().density;
   int padding_5dp = (int) (5 * scale + 0.5f);

//Define FrameLayout
       FrameLayout flTmp = new FrameLayout(this);
       LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT, 1f);
       lp.setMargins(0, 0, padding_5dp, padding_5dp);
       flTmp.setLayoutParams(lp);

//Add dynamically TextView
TextView tvTmp = new TextView(this);
tvTmp.setText("Test");
LinearLayout.LayoutParams tvPara=new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
tvPara.gravity = Gravity.BOTTOM | Gravity.LEFT;
tvPara.setMargins(padding_5dp, 0, 0, 0);
tvTmp.setLayoutParams(tvPara);

//Add dynamically Buttons for juice
    ImageButton btnTmp = new ImageButton(this);

[...]

//Add Button and TextView to FrameLayout
    flTmp.addView(btnTmp);
    flTmp.addView(tvTmp);

llRow1.addView(flTmp);

私がやろうとしているのは、次のxmlコードのようなテキストビューの説明を使用して画像ボタンを動的に作成することです。

<FrameLayout>
<ImageButton android:background="@null" android:id="@+id/button_x" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" android:src="@drawable/button_graphic"></ImageButton>    
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:clickable="false" android:text="TEST TEST"></TextView>
</FrameLayout>

私のコードでは問題なく動作しますが、textviewはマージンと重力のパラメーターを無視します。左上隅に余白なしで配置されています。

ヒントはありますか?

4

1 に答える 1

3

使用LayoutParamsする は、子が含まれる親のタイプである必要があります。 が含まFrameLayoutれていますが、参照をTextView割り当てています。LinearLayout.LayoutParamsにするFrameLayout.LayoutParamsか、 に変更しLinearLayoutます。推測する必要がある場合は、このエラーを黙ってキャッチし、無視しています。

于 2012-11-03T17:28:06.447 に答える