android基础:控件集锦

汇总常用的或者经常忘记的Android控件

DrawerLayout

弹出式抽屉视图
DrawerLayout 控件的子视图必须是2个,1个显示原视图,1个显示弹出的抽屉视图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.inthecheesefactory.lab.designlibrary.CodeLabActivity">
<!--视图1:原视图-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<!--视图2:弹出的抽屉视图 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffff"
android:orientation="vertical">

</LinearLayout>

硬按键控制音量

1 添加权限
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

2 新建控制音量类实例

1
2
 //音量控制,初始化定义
 private AudioManager mAudioManager;

3 实例初始化,在onCreat()中:

1
2
mAudioManager = (AudioManager) 
getSystemService(Context.AUDIO_SERVICE);

4 具体调节音量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
switch(keyCode)
{
case KeyEvent.KEYCODE_VOLUME_UP:
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume+1, 1);
return true;

case KeyEvent.KEYCODE_VOLUME_DOWN:
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume-1, 1);
return true;
}

return super.onKeyDown(keyCode, event);
}

home按键切换后台

1借用surface改变:
切换后台触发:

1
2
3
4
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  // TODO Auto-generated method stub
  Log.v("admob", "surfaceChanged!");
 }

回到前台触发:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void surfaceCreated(SurfaceHolder arg0) {	//activity resume
  // TODO Auto-generated method stub
  if(isPause && m_State == GAME_PLAY){
   AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity.mContext);
   builder.setTitle("Alert")
   .setMessage("Game is paused,press OK resume")
      .setCancelable(false)
      .setPositiveButton("OK", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        mLoopHandler.start();
       }
      });
   AlertDialog alert = builder.create();
   alert.show();
  }else{
   mLoopHandler.start();
  }
  isPause = false;
 }
```

## action bar使用
action bar是3.0之后系统自带的控件,

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aty_securityissue_creat);
actionBar= getActionBar();
actionBar.show();

}

1
2
3


//创建菜单

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
//添加菜单项
MenuItem add=menu.add(0,0,0,”add”);
MenuItem del=menu.add(0,0,0,”del”);
MenuItem save=menu.add(0,0,0,”save”);
//绑定到ActionBar
add.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
del.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
save.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}

//选择菜单
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
```

FragmentTabHost

参考案例