您的位置:首页 > 编程语言

代码编写布局

2014-05-08 14:25 399 查看
以前没有用代码写个布局文件,今天尝试了下,

很不熟悉,不过总算搞定,做笔记以防忘记!

package com.login.login.widget;

import com.login.login.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;

public class SpinnerTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// setContentView(R.layout.spinnertest);

ScrollView scrollView = new ScrollView(this);

LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(l);

int textViewResourceId = android.R.layout.simple_spinner_item;
creatspinner(textViewResourceId, l);
int textViewResourceId2 = android.R.layout.browser_link_context_header;
creatspinner(textViewResourceId2, l);
int textViewResourceId3 = android.R.layout.preference_category;
creatspinner(textViewResourceId3, l);
int textViewResourceId4 = android.R.layout.select_dialog_item;
creatspinner(textViewResourceId4, l);
int textViewResourceId5 = android.R.layout.select_dialog_multichoice;
creatspinner(textViewResourceId5, l);
int textViewResourceId6 = android.R.layout.select_dialog_singlechoice;
creatspinner(textViewResourceId6, l);
int textViewResourceId7 = android.R.layout.simple_dropdown_item_1line;
creatspinner(textViewResourceId7, l);
int textViewResourceId8 = android.R.layout.simple_expandable_list_item_1;
creatspinner(textViewResourceId8, l);
int textViewResourceId9 = android.R.layout.simple_gallery_item;
creatspinner(textViewResourceId9, l);
int textViewResourceId10 = android.R.layout.simple_list_item_1;
creatspinner(textViewResourceId10, l);
int textViewResourceId11 = android.R.layout.simple_list_item_checked;
creatspinner(textViewResourceId11, l);
int textViewResourceId12 = android.R.layout.simple_list_item_multiple_choice;
creatspinner(textViewResourceId12, l);
int textViewResourceId13 = android.R.layout.simple_list_item_single_choice;
creatspinner(textViewResourceId13, l);
int textViewResourceId14 = android.R.layout.simple_spinner_dropdown_item;
creatspinner(textViewResourceId14, l);
int textViewResourceId15 = android.R.layout.simple_spinner_item;
creatspinner(textViewResourceId15, l);
int textViewResourceId16 = android.R.layout.test_list_item;
creatspinner(textViewResourceId16, l);
setContentView(scrollView);
}

private void creatspinner(int style_layout, LinearLayout l) {
Spinner sp = new Spinner(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, style_layout);

adapter.add("red");
adapter.add("green");
adapter.add("yellow");
adapter.add("black");
adapter.add("write");
adapter.add("blue");

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);

LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
l.addView(sp, ll);
// setContentView(l);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: