如何运用相对布局(Relative Layout)
2011-08-30 16:37
309 查看
list.xml代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World, ex07" /> <ListView android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> |
public class Ex07Activity extends Activity implements OnItemClickListener { //WRAP_CONTENT = -2:Special value for the height or width requested by a View private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; //准备Adapter数据库 private String[] data = {" Item-1 ", " Item-2 ", " Item-3 "}; private TextView tv; private ArrayAdapter<String> adapter; private boolean[] status = {false, false, false }; @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); //相对布局对象 RelativeLayout r_layout = new RelativeLayout(this); //设置布局 setContentView(r_layout); //这个类被用于把一个XML文件实例化到相应的视图对象中。Context.LAYOUT_INFLATER_SERVICE = layout_inflater LayoutInflater inflate = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); //得到水平布局对象 LinearLayout layout = (LinearLayout)inflate.inflate(R.layout.list, null); //和相对布局有联系的每个子布局信息 RelativeLayout.LayoutParams param= new RelativeLayout.LayoutParams(175, WC); layout.setId(1); r_layout.addView(layout, param); tv = (TextView)layout.findViewById(R.id.text); //列表 ListView lv = (ListView)layout.findViewById(R.id.list); lv.setBackgroundResource(R.drawable.icon); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, data); lv.setAdapter(adapter); lv.setItemsCanFocus(false); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lv.setOnItemClickListener( this); Button btn1 = new Button(this); btn1.setBackgroundResource(R.drawable.icon); btn1.setId(2); btn1.setText("OK"); btn1.setOnClickListener((android.view.View.OnClickListener) listener); param= new RelativeLayout.LayoutParams(60, WC); param.addRule(RelativeLayout.RIGHT_OF, 1); param.leftMargin = 15; param.topMargin = 20; r_layout.addView(btn1, param); Button btn2 = new Button(this); btn2.setBackgroundResource(R.drawable.icon); btn2.setId(3); btn2.setText("Exit"); btn2.setOnClickListener((android.view.View.OnClickListener) listener); param= new RelativeLayout.LayoutParams(60, WC); param.addRule(RelativeLayout.BELOW, 2); param.addRule(RelativeLayout.ALIGN_LEFT, 2); param.topMargin = 25; r_layout.addView(btn2, param); } OnClickListener listener = new OnClickListener() { public void onClick(View v) { if(v.getId() == 2){ String ss = "{"; for(int i=0; i< adapter.getCount(); i++){ if(status[i]) { ss += data[i]; ss += " "; } } ss += "}"; setTitle(ss); }else if(v.getId() == 3) finish(); }}; public void onItemClick(AdapterView<?> arg0, View v, int idx, long arg3) { status[idx] = !status[idx]; } } |
相关文章推荐
- Android Studio如何把约束布局(ConstraintLayout)改为相对布局(RelativeLayout)
- 再学习---Android相对布局(RelativeLayout)
- 第13章、布局Layouts之RelativeLayout相对布局(从零开始学Android)
- Relativelayout(相对布局)示例
- 13 - 布局Layout:RelativeLayout相对布局
- RelativeLayout 相对布局
- Android开发中RelativeLayout相对布局
- Android RelativeLayout 相对布局解析
- Android-相对布局(RelativeLayout)
- RelativeLayout相对布局
- Xamarin.Forms的相对布局RelativeLayout
- RelativeLayout相对布局
- 「喵神成长史」『布局篇』RelativeLayout(相对布局)
- Android RelativeLayout相对布局
- 关于相对布局RelativeLayout的各种属性介绍
- 安卓布局:线性布局(Linearlayout)和相对布局(RelativeLayout)
- Android:控件布局(相对布局)RelativeLayout RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列。 相对布局常用属性: 子类控件相对子
- android RelativeLayout(相对布局)常用属性
- Android五大布局方式——相对布局(RelativeLayout)属性详解
- android 动态调整RelativeLayout布局控件的相对位置 layout_toLeftOf 之类的