0

アクティビティに、水平方向の Scrollview を追加しました。これには、[新しいセットを追加] ボタンと、以前に追加されたすべてのセットがボタンとして含まれています。これらのセットは SQLLite データベースに保存されます。

アプリの冒頭で、データベースからすべてのセットをロードします。Set ごとに、独自の Button をスクロールビューに追加します。

すべてのボタンが表示されますが、動的に追加されたボタンのサイズが正しくありません。「新しいセットを追加」ボタンと同じ高さと幅にする必要があります。

最初のボタンの寸法を他のボタンにコピーするにはどうすればよいですか?

ここに私のXML:

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:id="@+id/innerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_NewSet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:height="100dp"
            android:onClick="OnExitClick"
            android:text="@string/New_Set"
            android:width="100dp" />

    </LinearLayout>
</HorizontalScrollView>

ここで私のJavaコード:

 db.open();
 Cursor allSets = db.getAllSets();
 if (allSets.moveToFirst())
 {
    Button bDummy = (Button) findViewById(R.id.btn_NewSet);
    LinearLayout innerLayout = (LinearLayout) findViewById(R.id.innerLayout);
    do 
    {
          Button b1 = new Button(this);
          b1.setHeight(bDummy.getHeight());
          b1.setWidth(bDummy.getWidth());
          b1.setText(allSets.getString(1));
          b1.setLayoutParams(new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.WRAP_CONTENT,
              LinearLayout.LayoutParams.WRAP_CONTENT
              ));                   
          innerLayout.addView(b1);

    }while (allSets.moveToNext());
 }
 db.close();
4

2 に答える 2

0

動的ボタンも wrap_content を使用するため、同じサイズではありません。それらを同じサイズにしたい場合は、新しいボタンのlayoutparamsでボタン「id/btn_NewSet」の幅と高さのプロパティを使用できます

于 2013-03-20T15:39:25.720 に答える
0

動的ビュー インフレーションを使用してみてください。

1) 専用の xml (mybutton.xml など) を作成します。

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/btn_NewSet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:height="100dp"
        android:text="@string/New_Set"
        android:width="100dp" />

innerLayout2) 膨らませて動的にアタッチします。

    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout innerLayout = (LinearLayout) findViewById(R.id.innerLayout);
    do 
    {
        Button b1 = (Button)inflater.inflate(R.layout.mybutton,null);
        b1.setText(allSets.getString(1));
        innerLayout.addView(b1);
    }while (allSets.moveToNext());
于 2016-05-18T20:13:50.053 に答える