1. GridView
1) gridview.xml
2. SCrollView
ScrollView的作用是当一屏显示不完时,可以通过滚动来显示。
1)scroll.xml
2)ScrollActivity.java
1> LinearLayout.LayoutParams p 设置布局的参数, 调用addView方法时加入这个参数,就会按这个布局方式显示.
2> m_Layout.addView(textView,p); 往Layout布局中加入一个控件.
3> findViewById(R.id.button1). requestFocus(); 申请获得焦点.
4> Button.setOnClickListener 点击监听, buttonView.setOnKeyListener 按键监听.
1) gridview.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/gridview"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
></GridView>
2) GridActivity.java
public class GridActivity extends Activity{
private GridView m_gridview=null;
private SimpleAdapter mSimpleAdapter;
private List<Map<String, Integer>> mList = new ArrayList<Map<String, Integer>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
m_gridview=(GridView)findViewById(R.id.gridview);
m_gridview.setBackgroundResource(R.drawable.mm);
//设置Adapter
initSimpleAdapter();
m_gridview.setAdapter(mSimpleAdapter);
m_gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if(position==0)
{
Intent m_Inten1=new Intent(GridActivity.this,GalleryActivity.class);
startActivity(m_Inten1);
}
}
});
}
private void initSimpleAdapter() {
Field[] fields = R.drawable.class.getDeclaredFields(); // 取得drawable下的全部属性
// 循环取得的属性,以pic开头的都是要显示的图片,保存到list里面
for (Field field : fields) {
if (field.getName().startsWith("pic")) {
Map<String, Integer> map = new HashMap<String, Integer>();
try {
map.put("image", field.getInt(R.drawable.class));
} catch (Exception e) {
e.printStackTrace();
}
mList.add(map);
}
}
mSimpleAdapter = new SimpleAdapter(this, mList, R.layout.gallery_item,
new String[] { "image" }, new int[] { R.id.image });
}
}
实现的效果就和手机上的界面一个,一个个应用图标排列在主界面上。
2. SCrollView
ScrollView的作用是当一屏显示不完时,可以通过滚动来显示。
1)scroll.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scroll"
android:scrollbars="none">
<LinearLayout
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="textview"
android:id="@+id/textview1"
/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
ScrollView 中包含一个线性布局,这个线性布局中由一个TextView和一个Button组成,因此点击按钮时每次也增加一个这样的线性布局。
2)ScrollActivity.java
public class ScrollActivity extends Activity{
private LinearLayout m_Layout;
private ScrollView m_ScrollView;
private final Handler m_Handler=new Handler();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.scrollview);
m_Layout=(LinearLayout)findViewById(R.id.layout);
m_ScrollView=(ScrollView)findViewById(R.id.scroll);
Button m_Button=(Button)findViewById(R.id.button1);
m_Button.setOnClickListener(new Button.OnClickListener(){
private int mIndex=1;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView textView=new TextView(ScrollActivity.this);
textView.setText("Text view"+mIndex);
LinearLayout.LayoutParams p=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
m_Layout.addView(textView,p);
Button buttonView=new Button(ScrollActivity.this);
buttonView.setText("Button"+mIndex++);
m_Layout.addView(buttonView,p);
buttonView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN&&event.getAction()==KeyEvent.ACTION_DOWN)
{
findViewById(R.id.button1).requestFocus();
return true;
}
return false;
}
});
m_Handler.post(mScrollToButton);
}
});
m_Button.setOnKeyListener(mAddButtonKeyListener);
}
private Runnable mScrollToButton=new Runnable(){//定义线程体
@Override
public void run() {
// TODO Auto-generated method stub
int off=m_Layout.getMeasuredHeight()-m_ScrollView.getHeight();
if(off>0){
m_ScrollView.scrollTo(0, off);
}
}
};
private View.OnKeyListener mAddButtonKeyListener = new Button.OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
View viewToFucos=null;
if(event.getAction()==KeyEvent.ACTION_DOWN)
{
int iCount=m_Layout.getChildCount();
switch(keyCode)
{
case KeyEvent.KEYCODE_DPAD_UP:
if(iCount>0){
viewToFucos=m_Layout.getChildAt(iCount-1);
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if(iCount<m_Layout.getWeightSum())
{
viewToFucos=m_Layout.getChildAt(iCount+1);
}
break;
default:
break;
}
}
if(viewToFucos!=null)
{
viewToFucos.requestFocus();
return true;
}
else
return false;
}
};
}
实现的效果,每点一下button按钮,就会增加一个线性布局。
实现了动态的增加控件.
1> LinearLayout.LayoutParams p 设置布局的参数, 调用addView方法时加入这个参数,就会按这个布局方式显示.
2> m_Layout.addView(textView,p); 往Layout布局中加入一个控件.
3> findViewById(R.id.button1). requestFocus(); 申请获得焦点.
4> Button.setOnClickListener 点击监听, buttonView.setOnKeyListener 按键监听.