我正在尝试添加4 X ImageView
,每1 delay
之间{1}},
我的代码是:
final LinearLayout ll = (LinearLayout) findViewById(R.id.container);
for (int j=0;j<4;j++){
final ImageView newIMG = new ImageView(this);
newIMG.setLayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT,
1.0f));
newIMG.setImageResource(R.drawable.new_image);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
ll.addView(newIMG);
}
}, 1000);
}
但我得到的是 - 它等待一秒钟,然后display
一次ImageViews
......
(我希望他们一个接一个地展示)
答案 0 :(得分:1)
尝试:
final LinearLayout ll = (LinearLayout) findViewById(R.id.container);
for (int j=0;j<4;j++){
final ImageView newIMG = new ImageView(this);
newIMG.setLayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT,
1.0f));
newIMG.setImageResource(R.drawable.new_image);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
ll.addView(newIMG);
}
}, j*1000 + 1000);
}
答案 1 :(得分:0)
试试这个:
int id = 999;
final LinearLayout ll = (LinearLayout) findViewById(R.id.LinearLayout1);
for (int j=0;j<4;j++){
final ImageView newIMG = new ImageView(this);
newIMG.setImageResource(R.drawable.ic_launcher);
newIMG.setId(id);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 10, 0, 0);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
if(j > 0) {
params.addRule(RelativeLayout.BELOW, id - j);
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
ll.addView(newIMG);
}
}, (j+1)*1000);
newIMG.setLayoutParams(params);
id++;
}
答案 2 :(得分:0)
如果您不想让交错延迟同时运行大量线程,您可以执行以下操作:
public class MainActivity extends Activity {
private int viewsAddedCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setEnabled(false);
Thread thread = new delayedViewAdditionThread();
thread.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class delayedViewAdditionThread extends Thread
{
private android.os.Handler handler = new android.os.Handler()
{
@Override
public void handleMessage(android.os.Message msg)
{
switch(msg.what)
{
case 0 :
addTextView();
break;
case 1 :
Button b = (Button)findViewById(R.id.button1);
b.setEnabled(true);
break;
default :
break;
}
}
};
@Override
public void run()
{
for(int i = 0; i < 4; i++)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
handler.sendEmptyMessage(1);
}
private void addTextView()
{
TextView tv = new TextView(MainActivity.this);
tv.setText("TextView No. " + String.valueOf(viewsAddedCount + 1));
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout ll = (LinearLayout)findViewById(R.id.expandingLayout);
ll.addView(tv);
viewsAddedCount++;
}
}
}