Android中的基本控件(8)
- 安卓中的基本控件:滚动视图ScrollView,
首先我们了解一下什么是滚动视图
qq:
右边的滚动条就是利用这个原理做的,接下来我们自己来做一个简单的视图,首先定义xml文件
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ScrollView1" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:id="@+id/mylinear" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </ScrollView>
.java文件定义:
public class MainActivity extends Activity { private String data[]={"湖南大学","张穗穗","岳麓山下","爱晚亭","自卑亭的小卖部","www.ee.com","咨询邮箱","综合楼","中南大学","小TJ","天马公寓"}; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout layout=(LinearLayout)super.findViewById(R.id.mylinear); LinearLayout.LayoutParams param=new LinearLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);//定义布局参数 for(int i=0;i<this.data.length;i++){ Button bt=new Button(this); bt.setText(this.data[i]); layout.addView(bt,param); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
- 另一种 ListView
这回我们全部用代码布局,也就是说xml里不动手脚,这里要用到this.listview.setAdapter神秘适配器。源代码是:
public class MainActivity extends Activity { private String data[]={"湖南大学","张穗穗","岳麓山下","爱晚亭","自卑亭的小卖部","www.ee.com","咨询邮箱","综合楼","中南大学","小TJ","天马公寓"}; private ListView listview; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.listview=new ListView(this); this.listview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,this.data)); //定义适配器,每行显示一条数据,并设置组件内容 super.setContentView(listview); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
显示效果非常好,以前我们定义分割线要自己定义view,而这个主动定义了分割线。
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28