public class MyListViewActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
ListView listview = new ListView(this); listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData())); addContentView(listview, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Button button = new Button(this); button.setText("Change To MyListView2"); int left = listview.getLeft(); int top = listview.getTop()+listview.getHeight(); AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, left, top);
addContentView(button, params);
button.setOnClickListener(new OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(); intent.setClass(MyListViewActivity.this, MyListViewAvtivity2.class); startActivity(intent); } }); }
private List<String> getData(){
List<String> data = new ArrayList<String>(); data.add("测试数据1"); data.add("测试数据2"); data.add("测试数据3"); data.add("测试数据4");
return data; } }
为什么改变不了button的位置呢?
你看过listview的getHeight有没有值,按理解这时候应该还没有高度
先看看left top有值没有哦。说不好是0
真的都是0哦,不过这是为什么啊? listview是先于button加载到屏幕上的啊
android只有等create后,才计算所有的控件布局.
直接代码 layout(l,t,r,b);
public class MyListViewActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
final ListView listview = new ListView(this); listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData())); addContentView(listview,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final Button button = new Button(this); button.setText("Change To MyListView2");
ViewTreeObserver vto = listview.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){ public void onGlobalLayout(){ int left =listview.getLeft(); int top = listview.getTop()+listview.getHeight(); Log.v("left", (new Integer(left)).toString()); Log.v("top", (new Integer(top)).toString()); AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, left, top); addContentView(button, params); listview.getViewTreeObserver().removeGlobalOnLayoutListener(this); } });
button.setOnClickListener(new OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(); intent.setClass(MyListViewActivity.this, MyListViewAvtivity2.class); startActivity(intent); } }); }
private List<String> getData(){
List<String> data = new ArrayList<String>(); data.add("测试数据1"); data.add("测试数据2"); data.add("测试数据3"); data.add("测试数据4");
ListView可以通过addFooterView添加控件,用下面代码 public class MyListViewActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
final Button button = new Button(this); button.setText("Change To MyListView2");
final ListView listview = new ListView(this); LinearLayout layout = new LinearLayout(MyListViewActivity.this); layout.setOrientation(LinearLayout.HORIZONTAL); layout.addView(button); listview.addFooterView(layout); listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData())); addContentView(listview,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0));
ViewTreeObserver vto = listview.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){ public void onGlobalLayout(){ int left =listview.getLeft(); int top = listview.getTop()+listview.getHeight(); Log.i("left", (new Integer(left)).toString()); Log.i("top", (new Integer(top)).toString()); //AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, left, top); //addContentView(button, params); listview.getViewTreeObserver().removeGlobalOnLayoutListener(this); } }); button.setOnClickListener(new OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(); intent.setClass(MyListViewActivity.this, MyListViewActivity2.class); startActivity(intent); } }); }
private List<String> getData(){
List<String> data = new ArrayList<String>(); data.add("测试数据1"); data.add("测试数据2"); data.add("测试数据3"); data.add("测试数据4"); data.add("测试数据5"); data.add("测试数据6"); data.add("测试数据7"); data.add("测试数据8"); data.add("测试数据9"); data.add("测试数据10"); data.add("测试数据11");
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listview = new ListView(this);
listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));
addContentView(listview, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Button button = new Button(this);
button.setText("Change To MyListView2");
int left = listview.getLeft();
int top = listview.getTop()+listview.getHeight();
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, left, top);
addContentView(button, params);
button.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent();
intent.setClass(MyListViewActivity.this, MyListViewAvtivity2.class);
startActivity(intent);
}
});
}
private List<String> getData(){
List<String> data = new ArrayList<String>();
data.add("测试数据1");
data.add("测试数据2");
data.add("测试数据3");
data.add("测试数据4");
return data;
}
}
listview是先于button加载到屏幕上的啊
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listview = new ListView(this);
listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));
addContentView(listview,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final Button button = new Button(this);
button.setText("Change To MyListView2");
ViewTreeObserver vto = listview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
public void onGlobalLayout(){
int left =listview.getLeft();
int top = listview.getTop()+listview.getHeight();
Log.v("left", (new Integer(left)).toString());
Log.v("top", (new Integer(top)).toString());
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, left, top);
addContentView(button, params);
listview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
button.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent();
intent.setClass(MyListViewActivity.this, MyListViewAvtivity2.class);
startActivity(intent);
}
});
}
private List<String> getData(){
List<String> data = new ArrayList<String>();
data.add("测试数据1");
data.add("测试数据2");
data.add("测试数据3");
data.add("测试数据4");
return data;
}
}我改了下代码,在addOnGlobalLayoutListener中获取数据,可是还是不行额
public class MyListViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = new Button(this);
button.setText("Change To MyListView2");
final ListView listview = new ListView(this);
LinearLayout layout = new LinearLayout(MyListViewActivity.this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.addView(button);
listview.addFooterView(layout);
listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));
addContentView(listview,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0));
ViewTreeObserver vto = listview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
public void onGlobalLayout(){
int left =listview.getLeft();
int top = listview.getTop()+listview.getHeight();
Log.i("left", (new Integer(left)).toString());
Log.i("top", (new Integer(top)).toString());
//AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, left, top);
//addContentView(button, params);
listview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
button.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent();
intent.setClass(MyListViewActivity.this, MyListViewActivity2.class);
startActivity(intent);
}
});
}
private List<String> getData(){
List<String> data = new ArrayList<String>();
data.add("测试数据1");
data.add("测试数据2");
data.add("测试数据3");
data.add("测试数据4");
data.add("测试数据5");
data.add("测试数据6");
data.add("测试数据7");
data.add("测试数据8");
data.add("测试数据9");
data.add("测试数据10");
data.add("测试数据11");
return data;
}
}