private void setListPath(Context context, RelativeLayout footerRelativeLayout, String ButtonText) {
if (flagAddFooterGuide) {
// 我的音乐按钮
ImageView myMusic = new ImageView(context);
myMusic.setImageResource(R.drawable.style1_mymusic);
myMusic.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
((MyMusicActivity) myContext).setSdCardFlag(false);
((MyMusicActivity) myContext).onCreate(null);
}
});
myMusic.setId(1); // 设置ID为1
ImageView myStorage = new ImageView(context);
myStorage.setImageResource(R.drawable.style1_mymusic_usbstore);
myStorage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 设置ListView为显示
((MyMusicActivity) myContext).setlistViewLinearLayoutVisible(false);
// GalleryVisible为隐藏
((MyMusicActivity) myContext).setGalleryVisible(true);
}
});
myStorage.setId(2); // 设置id为2
// 设置本地当前目录的按钮
MyImageButton myImageButtonFolderNow = new MyImageButton(context, R.drawable.style1_mymusic_details, ButtonText);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.RIGHT_OF, 1);//在控件ID为1的控件的右边
RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp3.addRule(RelativeLayout.RIGHT_OF, 2);
footerRelativeLayout.addView(myMusic, lp1);
footerRelativeLayout.addView(myStorage, lp2);
// 播放按钮
MyImageButton myImageButtonPlay = new MyImageButton(context, R.drawable.mymusic_button_pause_backgroud, "播放");
footerRelativeLayout.addView(myImageButtonFolderNow, lp3);
myImageButtonPlay.setId(4);
RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp4.addRule(RelativeLayout.LEFT_OF, 5);
// 随机按钮
MyImageButton myImageButtonPlayRandom = new MyImageButton(context, R.drawable.mymusic_button_play_random, "随机开");
myImageButtonPlayRandom.setId(5);
RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp5.addRule(RelativeLayout.LEFT_OF, 6);
// 歌词按钮
MyImageButton myImageButtonLyric = new MyImageButton(context, R.drawable.mymusic_button_lyric, "LRC歌词");
myImageButtonLyric.setId(6);
RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp6.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// 加入
footerRelativeLayout.addView(myImageButtonLyric, lp6);
footerRelativeLayout.addView(myImageButtonPlayRandom, lp5);
footerRelativeLayout.addView(myImageButtonPlay, lp4);
flagAddFooterGuide = false;
案例:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
btn = new Button(this);
btn.setId(101);
btn.setText("test looper");
btn.setOnClickListener(this);
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(100,50);
param.topMargin = 10;
layout.addView(btn, param);
btn2 = new Button(this);
btn2.setId(102);
btn2.setText("exit");
btn2.setOnClickListener(this);
layout.addView(btn2, param);
tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setText("");
//其中 FP 是final int FP=LinearLayout.LayoutParams.FILL_PARENT;
//final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
LinearLayout.LayoutParams param2 =
new LinearLayout.LayoutParams(FP, WC);
param2.topMargin = 10;
param2.setMargins(0, 0, 5, 0); //参数分别是(int left, int top, int right, int bottom)
layout.addView(tv, param2);
setContentView(layout);