ボタンのクリック時に画像ボタンを動的に追加するにはどうすればよいですか? 画像ボタンを 2x3 形式で追加する必要があり、画面外に出たときに水平方向にスクロールできるようにする必要があります。
package com.example.dynamic;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
LinearLayout linearLayout1;
LinearLayout linearLayout2;
int flag=0;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2);
}
public void onClick(View v){
ImageView image = new ImageView(MainActivity.this);
image.setBackgroundResource(R.drawable.ic_launcher);
if(flag==0){
linearLayout1.addView(image);
flag=1;
}
else{
linearLayout2.addView(image);
flag=0;
}
}
}
これは私が使用したレイアウトです:
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Button" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearLayout1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearLayout2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
</LinearLayout>
これに伴う問題は、画像ボタンの数が画面幅を超えると、スクロールできないことです。もう 1 つの潜在的な問題は、2 つの行を一緒にではなく、別々にスクロールできる可能性があることです。そしてそれは望ましくありません。私を助けてください、私は初心者です。これを尋ねている間、間違いを許してください。