Android开发05—Android常用高级控件(下)
1. 滑块与进度条
1) ProgressBar类
ProgressBar类同样位于android.widget包下,但其继承自View,主要用于显示一些操作的进度。应用程序可以修改其长度表示当前后台操作的完成情况。因为进度条会移动,所以长时间加载某些资源或者执行某些耗时的操作时,不会使用户界面失去响应。
2) SeekBar类
SeekBar类继承自ProgressBar,是用来接收用户输入的控件。SeekBar类似于拖拉条,可以直观地显示用户需要的数据,常用于音量调节等场合。
3) 实例:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/linearLayout1">
<ProgressBar android:id="@+id/progressBar1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:max="100" android:progress="20" style="@android:style/Widget.ProgressBar.Horizontal"></ProgressBar>
<RatingBar android:layout_height="wrap_content" android:layout_width="wrap_content" android:max="5" android:rating="1" android:id="@+id/ratingBar1"></RatingBar>
</LinearLayout>
Activity:
package qijia.si;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.RatingBar;
public class JavaTest extends Activity {
/** Called when the activity is first created. */
final static double MAX = 100;
final static double MAX_STAR = 5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RatingBar rb = (RatingBar) findViewById(R.id.ratingBar1);
rb.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar1);
RatingBar rb = (RatingBar) findViewById(R.id.ratingBar1);
float rate = rb.getRating();
pb.setProgress((int)(rate/MAX_STAR*MAX));
}
});
}
}
2. 选项卡
TabHost类位于android.widget包下,是选项卡的封装类,用于创建创建选项卡的窗口。TabHost继承自FrameLayout是帧布局的一种。
实例:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="vertical">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageView1" android:scaleType="fitXY" android:layout_gravity="center" android:src="@drawable/andy"></ImageView>
<TextView android:layout_height="wrap_content" android:id="@+id/textView1" android:layout_width="wrap_content" android:text="@string/andy" android:textSize="24dip"></TextView>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="vertical" android:id="@+id/linearLayout2">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView2" android:scaleType="fitXY" android:layout_gravity="center" android:src="@drawable/bill"></ImageView>
<TextView android:layout_height="wrap_content" android:text="@string/bill" android:layout_width="wrap_content" android:id="@+id/textView2" android:textSize="24dip"></TextView>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:gravity="center_horizontal" android:layout_height="fill_parent" android:id="@+id/linearLayout3">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageView3" android:scaleType="fitXY" android:layout_gravity="center" android:src="@drawable/torvalds"></ImageView>
<TextView android:layout_height="wrap_content" android:text="@string/linus" android:layout_width="wrap_content" android:id="@+id/textView3" android:textSize="24dip"></TextView>
</LinearLayout>
</FrameLayout>
Activity:
package qijia.si;
import android.app.Activity;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TabHost;
public class JavaTest extends TabActivity {
/** Called when the activity is first created. */
private TabHost myTabhost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myTabhost = this.getTabHost();
LayoutInflater.from(this).inflate(R.layout.main, myTabhost.getTabContentView(),true);
myTabhost.addTab(myTabhost.newTabSpec("选项卡1").setIndicator("选项卡1",getResources().getDrawable(R.drawable.png1)).setContent(R.id.linearLayout1));
myTabhost.addTab(myTabhost.newTabSpec("选项卡2").setIndicator("选项卡2",getResources().getDrawable(R.drawable.png2)).setContent(R.id.linearLayout2));
myTabhost.addTab(myTabhost.newTabSpec("选项卡3").setIndicator("选项卡3",getResources().getDrawable(R.drawable.png3)).setContent(R.id.linearLayout3));
}
}
3. 画廊控件
Gallery是一种水平滚动的列表,一般情况下用来显示图片等资源,可以使图片在屏幕上滑来滑去。Gallery所显示的图片资源同样来自适配器。
Gallery是View的子类,Gallery控件可以在XML中配置,也可以再代码中操作。
实例:将需要显示的控件存放在BaseAdapter中,然后在适当的时间将此BaseAdapter设置Gallery控件使之显示。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/linearLayout1" android:gravity="center_vertical">
<Gallery android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/gallery1" android:spacing="10dip" android:unselectedAlpha="1"></Gallery>
</LinearLayout>
Activity:
package qijia.si;
import android.app.Activity;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TabHost;
public class JavaTest extends Activity {
/** Called when the activity is first created. */
int[] imageIds={
R.drawable.bbta,R.drawable.bbtb,R.drawable.bbtc,
R.drawable.bbtd,R.drawable.bbte,R.drawable.bbtf,
R.drawable.bbtg
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g1 = (Gallery)this.findViewById(R.id.gallery1);
BaseAdapter ba = new BaseAdapter(){
public int getCount() {
// TODO Auto-generated method stub
return imageIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv = new ImageView(JavaTest.this);
iv.setImageResource(imageIds[position]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(188,250));
return iv;
}
};
g1.setAdapter(ba);
g1.setOnItemClickListener(
new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Gallery g1 = (Gallery)findViewById(R.id.gallery1);
g1.setSelection(arg2);
}
});
}
}