1

実行時に TableRow にいくつかのボタンを追加しようとしています。私のTableRowには次の構造があります:

    <TableRow 
            android:layout_marginTop="100px" 
            android:gravity="bottom" 
            android:paddingTop="50px" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/riga1">

            <Button 
                android:textSize="32px" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:id="@+id/buttonOK2" 
                android:text="YES"
                android:onClick="setResult"/>
        </TableRow>

そして私のJavaコードは次のとおりです。

TableRow tr = (TableRow) findViewById(R.id.riga1);
        Button b = new Button(this);  
        b.setText("button 1");  
        b.setId(1);
        b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));   
        tr.addView(b);

        Button b1 = new Button(this);  
        b1.setText("button 2");  
        b1.setId(2);
        b1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));   
        tr.addView(b1);

しかし、これはうまくいかないようです。私のアクティビティでは、xml ファイルで定義されたボタンしか表示されません。

別の問題は、関連する TableLayout にパラメーターを設定すると、次のようになることです。

android:stretchColumns="*" 

前述の TableLayout の TableRow に少なくとも 1 つのボタンを (xml ファイルに) 挿入する必要があります。そうしないと、streatchColumns の近くで「0 による除算」エラーが発生します。tableRow を空にするにはどうすればよいですか?

4

3 に答える 3

1

あなたの問題は、LayoutParamsあなたの s に間違ったオブジェクトを使用していることだと思います。TextViewこれを試してください:

 TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT); 

次に、このlayoutParamsオブジェクトを設定します。

 b.setLayoutParams(layoutParams);
于 2013-04-04T12:05:32.520 に答える
0

テーブル レイアウトの代わりに、相対レイアウトを使用する必要があります。試してみてください

このようなXMLファイルで

<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"
android:id="@+id/myMainRelativeLayout"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/btnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="47dp"
    android:text="Add Button" />

そして、怒鳴るようなあなたのJavaコード

public class MainActivity extends Activity implements
    android.view.View.OnClickListener {

Button btnAdd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.btnAdd) {
        RelativeLayout rl = (RelativeLayout)findViewById(R.id.myMainRelativeLayout);
        Button btn = new Button(this);
        btn.setId(1234);
        btn.setText("Add Button Runtime");
        btn.setOnClickListener(this);
        rl.addView(btn);
    }
    if(v.getId() == 1234)
    {
        Toast.makeText(getApplicationContext(), "This is Your New Button", Toast.LENGTH_LONG).show();

    }
}

}

これを試してみてください、それは本当にうまくいきます

于 2014-05-11T17:36:35.377 に答える
-1

まず、このステートメントを使用してボタンを見つける必要があります

btn=(ボタン)findViewById(R.id.Button1);

これを使用して可視性を設定するよりも:

btn.setVisibility(View.INVISIBLE);

次に、次のステートメントを使用して可視性を設定できます。

btn.setVisibility(View.VISIBLE);

于 2013-04-04T12:02:42.577 に答える