每次都忘记,记下来,以后方便查找,代码是从现在的项目中Copy出来的,先来个relativeLayout的
- 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的
?[Copy to clipboard]
Download zuiniuwang.java
- super.onCreate(icicle);
- 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);
一定要记住,创建的Parameter addview时,一定是这个子控件的参数,而不是这个容器本身的参数,这个相当重要,我在此处浪费了不少时间。如果需要对本Layout加入上参数可以 layout.setLayoutParams( XXX params) 和 layout.setXXX 便可。