在Fragment中添加一个布局容器,并设置ID,在Activity中findbyview找到后,就可以动态添加了。
代码如下:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/first_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/first_button"/>
<Button
android:id="@+id/second_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second_button"/>
<Button
android:id="@+id/third_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/third_button"/>
</LinearLayout>
一、activity
1.一个activity就是一个类,继承activity;
2.需要复写onCreate()方法;
3.每一个activity都需要在AndroidMainfest.xml清单上进行配置;
4.为activity添加必要的控件。
二、布局
线性布局:LinearLayout
1.填满父空间:fill_parent、match_parent
2.文本多大空间就有多大:warp_content
3.文字对齐方式:gravity
4.占屏幕的比例:layout_weight="1" 水平方向,则width=0,垂直方向,则height=0
5.一行显示,空间不够会省略:singleLine="ture" false会换行
6.背景:background="#ffffff"
7.水平布局:orientation="horizontal"
垂直布局:orientation="vertivcal"
表格布局:TableLayout
1.内边距:padding
2.外边距:marginLeft\Start、Right\End、Top、Bottom
三、RelativeLayout相对布局
layout_above 将该控件的底部置于给定ID控件之上
layout_below 将该控件的顶部置于给定ID控件之下
layout_toLeftOf 将该控件的右边缘和给定ID控件的左边缘对齐
layout_toRightOf 将该控件的左边缘和给定ID控件的右边缘对齐
layout_alignBaseline 该控件的baseline和给定ID的控件的Baseline对齐
layout_alignBottom 该控件的底部边缘和给定ID的控件的底部边缘对齐
layout_alignLeft 该控件的左边缘和给定ID的控件的左边缘对齐
layout_alignRight 该控件的右边缘和给定ID的控件的右边缘对齐
layout_alignTop 该控件的顶部边缘和给定ID的控件的顶部边缘对齐
layout_alignparentBottom 如果该值为true,则该控件的底部和父控件的底部对齐layout_alignParentLeft 如果该值为true,则该控件的左边和父控件的左边对齐
layout_alignParentRight 如果该值为true,则该控件的右边和父控件的右边对齐
layout_alignParentTop 如果该值为true,则该控件的上边和父控件的上边对齐
layout_centerHorizontal 如果该值为true,则该控件将被置于水平方向的中央
layout_centerInParent 如果该值为true,则该控件将被置于父控件水平和垂直方向的中央
layout_centerVertival 如果该值为true,则该控件将被置于垂直方向的中央
四、一个Intent对象包含一组信息
1.Component name
2.Action
3.Data
4.Category
5.Extras
6.Flags
Intent intent = new Intent(this, SecondActivity.class)
startActivity(intent) //startActivity方法
intent.putExtra("Key", "Value") //键值对
intent = getIntent()
String value = intent.getStringExtra("Key") //通过键提取数据
五、初级控件:EditText、TextView、Button
1.获取EditText的值
String value = EditText.getText().toString()
2.将值放到Intent对象中
Intent intent = new Intent()
intent.putExtra("one",value )
intent.setCalss(Activity.this, OtherActivity.class)
3.使用这个Intent对象来启动Otheractivity
Activity.this.startActivity(intent)
4.将监听器的对象绑定到按钮对象上
button.setOnclickListener(new Listener())
5.得到Intent对象当中的值
Intent intent = getIntent()
String value1 = intent.getStringExtra("one")
int value2 = Integer.parseInt(value)
六、其他初级控件使用
①ImageView
②RadioGroup和RadioButton
setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener())
③Checkbox
setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener())
④Menu
1.当客户点击MENU按钮的时候,调用onCreateOptionMenu()方法
public boolean onCreateOptionMenu(Menu menu){
menu.add(0,1,1,R.string.id)
}
2.当客户点击MENU内部的具体某一个选项时,调用onOptionItemSelected()方法
public boolean onOptionItemSelected(MenuItem item){
if(item.getItemId() == 1){
finish()
}
return super.onOptionItemSelected(item)
}
七、Activity的生命周期
1.第一次创建时调用
protected void onCreat(Bundle saveInstanceState)
2.显示出来时调用
protected void onStrat()
3.获得用户焦点时调用(可 *** 作)
protected void onResume()
4.点击d出第二个Activity时调用
protected void onPause()
5.当第一个Activity不可见时调用
protected void onStop()
6.当返回第一个Activity时调用,代替OnCreate,因为没被销毁
protected void onRestart()
7.当返回第一个Activity时调用(先执行onStop,在执行,因为第二个Activity被销毁,不能返回获取,只能通过onCreat,onStart,onResume再创建)
protected void onDestory()
八、Task
1.Task是存放Activity的Stack栈。当点击启动第二个Activiry时,第一个Activtiy会被压入Stack栈当中,第二个Activity会位于栈的顶部;当返回第一个Activtiy时,第二个Activity会被d出Stack,第一个Activity会位于栈的顶部,以此类推。
注释:当调用finish()时,当前的Activity会被Destory掉,栈中的Activity会消失。
2.当Activity都从Stack退出后,则就不存在Task。
九、高级控件
①进度条ProgressBar
水平进度条style="?android:attr/progressBarStyleHorizontal"
圆圈进度条style="?android:attr/progressBarStyle"
用户可视的visibility="gone"
②列表ListView
十、其他控件
A.下拉菜单Spinner
1.创建一个ArrayAdapter:
ArrayAdapter<CharSequence>adapter = ArrayAdapter.createFromResource(
this, //指上下文对象
R.array.plant_array, //引用了在文件中定义的String数组
android.R.layout.simple_spinner_item)//用来指定Spinner的样式,可替换自定义
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item)//设置Spinner当中每个条目的样式
2.得到Spinner对象,并设置数据:
spinner=(spinner)findViewById(R.id.spinnerId)
spinner.setAdapter(adapter)
spinner.setPrompt("测试")//标题
3.创建监听器
class SpinnerOnSelectListener implements OnItemSelectedListener{
@override
public void onItemSelected(
AdapterView<?>adapterView,//整个列表对象
View view,//被选中的具体条目对象
int position,//位置
long id){ //id
String selected = adapterView.getItemAtPosition(position).toString()
}
@override
public void onNothingSelected(AdapterView<?>adapterView){
S.o.p("nothingSelected")
}
}
4.绑定监听器
spinner.setOnItemSelectedListener(new SpinnerOnSelectListener())
注:第二种动态设计
1.创建ArrayList对象
List<String>list = new ArrayList<String>()
list.add("test1")
2. 调用方法
ArrayAdapter adapter = new ArrayAdapter(
this, //指上下文对象
R.layout.item, //引用了指定了下拉菜单的自定义布局文件
R.id.textViewId,//id
list)//数据
3.得到Spinner对象,并设置对象
spinner.setAdapter(adapter)
spinner.setPrompt("测试")//标题
3.创建监听器
class SpinnerOnSelectListener implements OnItemSelectedListener{
@override
public void onItemSelected(
AdapterView<?>adapterView,//整个列表对象
View view,//被选中的具体条目对象
int position,//位置
long id){ //id
String selected = adapterView.getItemAtPosition(position).toString()
}
@override
public void onNothingSelected(AdapterView<?>adapterView){
S.o.p("nothingSelected")
}
}
4.绑定监听器
spinner.setOnItemSelectedListener(new SpinnerOnSelectListener())
B.DatePicker和DatePickerDialog
1.声明一个监听器,使用匿名内部类
DatePickerDialog.OnDateSetListener onDateSetListener
= new DatePivkerDialog.OnDateSetListener(){
public void onDateSet(
DatePicker view,
int year,
int monthOfYear,
int dayOfMonth){
S.o.p(year+"-"+motnOfYear+"-"+dayOfMonth)
}
}
2.复写onCreateDialog(int id)方法:
@override
protected Dialog onCreateDialog(int id){
switch(id){
case DATE_PICKER_ID:
return new DatePickerDialog(this,onDateSetListener,2019,11,25)
}
return null
}
3.使用时调用showDialog()方法
showDialog(DATE_PICKER_ID)
C.AutoCompleteTextView
B.Widget
C.Animatin
十一、实现ContentProvider过程
1.定义一个CONTENT_URI常量
2.定义一个类,继承ContentProvider
3.实现query、insert、update、delete、getType和onCreate方法
4.在AndroidManifest.xml当中进行声明
动态增加控件的基本思路就是:实例化控件->布局.addView(控件) ->OnCreat中绑定布局控件 setContentView(布局)例1:在LinearLayout中动态增加Button,EditText等控件,并且点击Button后,动态增加EditText等控件。
Java代码
1.@Override
2.protected void onCreate(Bundle savedInstanceState) {
3.super.onCreate(savedInstanceState)
4.final LinearLayout linearLayout=new LinearLayout(this)
5.linearLayout.setOrientation(LinearLayout.VERTICAL)
6.
7.LinearLayout linearLayout1 = new LinearLayout(this)
8.linearLayout1.setOrientation(LinearLayout.VERTICAL)
9.
10.Button button1= new Button(this)
11.button1.setText("增加新项目")
12.
13.Button button2 = new Button(this)
14.button2.setText("增加新组")
15.
16.linearLayout.addView(button1)
17.linearLayout.addView(button2)
18.
19.//设定按钮单击监听器,动态增加控件.
20.button2.setOnClickListener(new Button.OnClickListener(){
21.public void onClick(View v) {
22.Context context = v.getContext()
23.LinearLayout linearLayout2 = new LinearLayout(context)
24.linearLayout2.setOrientation(LinearLayout.HORIZONTAL)
25.
26.EditText groupNameEditText = new EditText(context)
27.//设置控件大小.
28.groupNameEditText.setWidth(160)
29.
30.ImageButton imagebutton1 = new ImageButton(context)
31.Button saveButton = new Button(context)
32.Button exitButton = new Button(context)
33.
34.linearLayout2.addView(groupNameEditText)
35.linearLayout2.addView(imagebutton1)
36.linearLayout2.addView(saveButton)
37.linearLayout2.addView(exitButton)
38.linearLayout.addView(linearLayout2)
39.
40.}
41.})
42.
43.setContentView(linearLayout)
44.}
45.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)