複数行のボタン リストを作成したいと考えています。このようなもの:
しかし、動的に(コードで)実行したい。これを自動的に行うようにレイアウトに指示する方法はありますか? または、を使用して自分でこれを行う必要がありますRelativeLayout.LayoutParams
。コードでこれを行うことができますが、非常に多くのことを制御する必要があるため、これを行う別の簡単な方法があるかどうか疑問に思っていました. たとえば、現在の行がいっぱいになったときに次の行に要素を追加するようにレイアウトに指示します。
3 に答える
テーブル レイアウトを使用することもできます。
このようにxmlでtabllayoutの最初の行を作成します
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/goBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginTop="12dp"
/>
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80"
android:background="#f0ffff" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="@+id/data_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#006400"
android:orientation="vertical" >
<TableRow
android:id="@+id/second"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="@drawable/textbg"
android:gravity="center"
android:text="Number1"
android:textColor="#006400" >
</TextView>
<Button
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="@drawable/textbg"
android:gravity="center"
android:text="Number2"
android:textColor="#006400" />
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="@drawable/textbg"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:text="Distance"
android:textColor="#006400" >
</TextView>
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="5dp"
android:paddingTop="8dp"
android:text="F/G/H/S"
android:textColor="#006400" >
</TextView>
</TableRow>
</TableLayout>
</ScrollView>
</FrameLayout>
</HorizontalScrollView>
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/savescore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
4 つの 3 つのテキストに対してこの xml を作成し、1 行のボタンで onCreate のテーブルを参照します。
TableLayout extendeedTable = (TableLayout)findViewById(R.id.data_table);
のような行を追加します
while (extendeedTable.getChildCount() > 1)
{
// while there are at least two rows in the table widget, delete
// the second row.
extendeedTable.removeViewAt(1);
}
// collect the current row information from the database and
// store it in a two dimensional ArrayList
// iterate the ArrayList, create new rows each time and add them
// to the table widget.
// ここで値は、テーブルに必要な行数です
for (int position=0; position < value ; position++)
{
TableRow tableRow= new TableRow(this);
tableRow.setBackgroundDrawable(null);
// ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(Integer.toString(position + 1));
idText.setGravity(Gravity.CENTER);
idText.setTextColor(Color.BLACK);
idText.setWidth(10);
idText.setHeight(45);
idText.setBackgroundResource(R.drawable.text2);
tableRow.addView(idText);
textOne = new Button(this);
textOne.setText("CLUB");
textOne.setBackgroundResource(R.drawable.text2);
textOne.setGravity(Gravity.CENTER);
textOne.setTextColor(Color.BLACK);//left top right bottom
textOne.setWidth(10);
textOne.setHeight(45);
textOne.setId(1+position);
tableRow.addView(textOne);
allbtns.add(textOne);
// textOne.setOnClickListener(this);
textOne.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// do something when the button is clicked
final Button button = (Button) arg0;
System.out.println("value of button is "+
button.getId());
dialog1.setTitle(" SELECT CLUB ");
textTwo = new EditText(this);
textTwo.setText("");
textTwo.setBackgroundResource(R.drawable.text2);
textTwo.setGravity(Gravity.CENTER);
textTwo.setTextColor(Color.BLACK);
textTwo.setWidth(10);
textTwo.setHeight(45);
textTwo.setInputType(InputType.TYPE_CLASS_NUMBER);
tableRow.addView(textTwo);
allEds1.add(textTwo);
textTwo.setId(position +1);
textThree = new EditText(this);
textThree.setText("");
textThree.setWidth(10);
textThree.setHeight(45);
textThree.setTextColor(Color.BLACK);
textThree.setBackgroundResource(R.drawable.text2);
textThree.setGravity(Gravity.CENTER);
textThree.setInputType(InputType.TYPE_CLASS_TEXT);
tableRow.addView(textThree);
allEds2.add(textThree);
textThree.setId(position +1);
extendeedTable.addView(tableRow);
}
このために、私はここから助けを借りました
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/7/
と
そのxml
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
さらにGoogle検索を行った後、最終的にこれを行う最良の方法を見つけました。アダプターとグリッドを使用して、とてもクリーンでシンプルです。すべての答えをありがとう
ここにチュートリアルがあります:Gridview(ButtonAdapter)用のカスタムアダプターの作成
また、LinearLayout を使用してこれを行い、重みを使用してすべてのボタンを同じサイズにすることもできます。
あなたの質問について:
コードでこれを行うことができますが、非常に多くのことを制御する必要があり、これを行う別の簡単な方法があるかどうか疑問に思っていました。たとえば、現在の行がいっぱいになったときに次の行に要素を追加するようにレイアウトに指示します。
これは、画面の幅と高さを測定し、View クラスの関数を使用してその特定のビューとその子の詳細を把握する場合に可能になる可能性があります。
別
ただし、コメントで述べたように、GridView などの問題を解決するために使用できる他のビューがあります。