android ui开发高级,Android开发笔记(四)高级UI组件

ProgressBar

style="" 改进度条样式,前面无android:

android:max="100" 最大进度

android:progress="40" 当前进度

public class MainActivity extends AppCompatActivity {

private ProgressBar pb;

private int p = 0;

private Handler mHandler;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

pb = findViewById(R.id.pb);

mHandler = new Handler(){

@Override

public void handleMessage(@NonNull Message msg) {

if(msg.what == 0x111){

pb.setProgress(p);

}else {

Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();

pb.setVisibility(View.GONE);

}

}

};

new Thread(new Runnable() {

@Override

public void run() {

while (true){

p = doWork();

Message m = new Message();

if(p < 100){

m.what = 0x111; // 自定义消息代码,0x***

mHandler.sendMessage(m);

}else{

m.what = 0x110;

mHandler.sendMessage(m);

break;

}

}

}

private int doWork(){

p += Math.random() * 10;

try {

Thread.sleep(200);

} catch (InterruptedException e) {

e.printStackTrace();

}

return p;

}

}).start();

}

}

SeekBar

ProcessBar子类

android:thumb="" 拖动图标

580470a09c7d

public class MainActivity extends AppCompatActivity {

private SeekBar sb;

private ImageView iv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

sb = findViewById(R.id.sb);

iv = findViewById(R.id.iv);

sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

@Override

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

iv.setImageAlpha(progress);

}

@Override

public void onStartTrackingTouch(SeekBar seekBar) {

Toast.makeText(MainActivity.this, "start", Toast.LENGTH_SHORT).show();

}

@Override

public void onStopTrackingTouch(SeekBar seekBar) {

Toast.makeText(MainActivity.this, "end", Toast.LENGTH_SHORT).show();

}

});

}

}

RatingBar

android:numStars="5" 最大星星数量

android:rating="0" 默认星星数量

android:stepSize="0.5" 星星step

android:isIndicator="false" true则不能改变星星

getRating | getNumStars | getStepSize

ImageView

android:src="" 图片文件

android:scaleType="" 缩放方式

fitXY 填充拉伸

center 居中,不缩放,小则截取

centerCrop 锁定长宽比缩放,截取显示

centerInside 锁定长宽比缩放,完全显示

fitCenter 同上,位于中央,类似还有fitEnd底部, fitStart顶部

martix 矩阵绘制

android:adjustViewBounds="true" 自调整图片layout, 需要layout用wrap

android:maxWidth="90dp" 最大宽

android:maxHeight="90dp" 最大高

android:tint="#aaff0000" 滤镜,蒙版

ImageSwitcher

点击切换

public class MainActivity extends AppCompatActivity {

ImageSwitcher is;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

is = findViewById(R.id.is);

is.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_out));

is.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in));

is.setFactory(new ViewSwitcher.ViewFactory() {

@Override

public View makeView() {

ImageView iv = new ImageView(MainActivity.this);

iv.setImageResource(R.drawable.i1);

return iv;

}

});

is.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

((ImageSwitcher)v).setImageResource(R.drawable.i3);

}

});

}

}

滑动切换:

public class MainActivity extends AppCompatActivity {

private int[] arrayP = new int[]{R.drawable.i0,R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4,R.drawable.i5,};

private ImageSwitcher is;

private int index;

private float touchDownX;

private float touchUpX;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

is = findViewById(R.id.is);

is. setFactory(new ViewSwitcher.ViewFactory() {

@Override

public View makeView() {

ImageView iv = new ImageView(MainActivity.this);

iv.setImageResource(arrayP[index]);

return iv;

}

});

is.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

if(event.getAction() == MotionEvent.ACTION_DOWN){

touchDownX = event.getX();

return true;

}else if(event.getAction() == MotionEvent.ACTION_UP){

touchUpX = event.getX();

if(touchUpX - touchDownX > 100){

index = index == 0? arrayP.length - 1: index - 1;

is.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left));

is.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_out_right));

is.setImageResource(arrayP[index]);

}else if(touchUpX - touchDownX < -100){

index = index == arrayP.length - 1? 0: index + 1;

is.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in));

is.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_out));

is.setImageResource(arrayP[index]);

}

return true;

}

return false;

}

});

}

}

GridView

android:numColumns="" 列数

Adapter

ArrayAdapter

SimpleAdapter

SimpleCursorAdapter

BaseAdapter

采用SimpleAdapter

580470a09c7d

//新建一个cell 的layout, img是cell里的ImageView

public class MainActivity extends AppCompatActivity {

private GridView gv;

private int[] arrayP= new int[]{R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4,R.drawable.i5,R.drawable.i6,R.drawable.i7,R.drawable.i8,R.drawable.i9};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

gv = findViewById(R.id.gv);

List> listitem = new ArrayList>();

for(int i = 0; i < arrayP.length; i++){

Map map = new HashMap();

map.put("image", arrayP[i]); // image这个key存在,替换掉value,返回原value虽然没用

listitem.add(map); // add后原map就没用了,呈现 List[dict]格式保存在listitem里

} // 为了构造适用于Adapter的List

SimpleAdapter simpleAdapter = new SimpleAdapter(this, listitem, R.layout.cell, new String[]{"image"}, new int[]{R.id.img});

gv.setAdapter(simpleAdapter);

}

}

580470a09c7d

public class MainActivity extends AppCompatActivity {

private GridView gv;

private int[] arrayP= new int[]{R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4,R.drawable.i5,R.drawable.i6,R.drawable.i7,R.drawable.i8,R.drawable.i9};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

gv = findViewById(R.id.gv);

// List> listitem = new ArrayList>();

// for(int i = 0; i < arrayP.length; i++){

// Map map = new HashMap();

// map.put("image", arrayP[i]); // image存在,替换掉value

// listitem.add(map); // add后原map就没用了,呈现 List[dict]格式保存在listitem里

// } // 为了构造适用于Adapter的List

// SimpleAdapter simpleAdapter = new SimpleAdapter(this, listitem, R.layout.cell, new String[]{"image"}, new int[]{R.id.img});

// gv.setAdapter(simpleAdapter);

gv.setAdapter(new ImageAdapter(this));

}

public class ImageAdapter extends BaseAdapter{

private Context mContext;

public ImageAdapter (Context c){

mContext = c;

}

@Override

public int getCount() {

return arrayP.length; // 记得改

}

@Override

public Object getItem(int position) {

return null;// 记得改

}

@Override

public long getItemId(int position) {

return 0;// 记得改

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ImageView iv;

if(convertView == null){

iv = new ImageView(mContext);

iv.setLayoutParams(new GridView.LayoutParams(100,90));

iv.setScaleType(ImageView.ScaleType.CENTER_CROP);

}else {

iv = (ImageView) convertView;

}

iv.setImageResource(arrayP[position]);

return iv;

}

}

}

Spinner

android:entries="" 列表内容

列表内容定义方法:

//(1)数组资源文件 *.xml

全部

游戏

电影

动漫

//(2)Adapter

String[] cin = new String[]{"全部","音乐","美术","新闻"};

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, cin);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Spinner sp = findViewById(R.id.sp);

sp.setAdapter(adapter);

获取值

580470a09c7d

sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

@Override

public void onItemSelected(AdapterView> parent, View view, int position, long id) {

String str = sp.getSelectedItem().toString();

Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();

}

@Override

public void onNothingSelected(AdapterView> parent) {

}

});

ListView

android:entries="" 列表内容

xml同上,或采用java显示:

String[] cin = new String[]{"一","二","三"};

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, cin);

ListView lv = findViewById(R.id.lv);

lv.setAdapter(adapter);

580470a09c7d

// 需要新建一个mylayout放置每一行的组件

public class MainActivity extends AppCompatActivity {

private int[] arrayP= new int[]{R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4,R.drawable.i5,R.drawable.i6,R.drawable.i7,R.drawable.i8,R.drawable.i9};

String[] str = new String[]{"1","2","3","4","5","6","7","8","9"};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

List> mylist = new ArrayList>();

for(int i = 0; i< arrayP.length; i++){

Map map = new HashMap();

map.put("image", arrayP[i]);

map.put("name", str[i]);

mylist.add(map);

}

SimpleAdapter adapter = new SimpleAdapter(this, mylist, R.layout.mylayout, new String[]{"name","image"}, new int[]{R.id.t1, R.id.img1});

ListView lv = findViewById(R.id.lv);

lv.setAdapter(adapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView> parent, View view, int position, long id) {

Map map = (Map)parent.getItemAtPosition(position);

Toast.makeText(MainActivity.this, map.get("name").toString(), Toast.LENGTH_SHORT).show();

}

});

}

}

练习时有资源错误,另开笔记写了解决方案

ScrollView

垂直滚动,水平用HorizontalScrollView

xml中一个滚动组件只能放一个子组件,可以用布局管理器包括起来

java中创建滚动条:

LinearLayout ll = findViewById(R.id.ll);

LinearLayout ll2 = new LinearLayout(MainActivity.this);

ll2.setOrientation(LinearLayout.VERTICAL);

ScrollView sv = new ScrollView(MainActivity.this);

ll.addView(sv); // 把滚动视图加入主视图

sv.addView(ll2); // 把一个线性管理器加入滚动视图

ImageView iv = new ImageView(MainActivity.this);

iv.setImageResource(R.drawable.i1);

iv.setAdjustViewBounds(true);

ll2.addView(iv); // 把一张图片加入该线性管理器

TextView tv = new TextView(MainActivity.this);

tv.setText(R.string.context);

ll2.addView(tv); // 把文字加入线性管理器

TabHost, TabWidget, TabContent

选项卡

(1)编辑布局文件Main

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@android:id/tabhost"

tools:context=".MainActivity">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@android:id/tabs"

/>

android:id="@android:id/tabcontent"

android:layout_width="match_parent"

android:layout_height="match_parent"

/>

(2)做每个选项卡的Layout,并设置ID,这里设置了两个:

文件名为tab1和tab2,id为left和right

然后java:

TabHost th = findViewById(android.R.id.tabhost);

th.setup();

LayoutInflater inf = LayoutInflater.from(this);

inf.inflate(R.layout.tab1, th.getTabContentView());

inf.inflate(R.layout.tab2, th.getTabContentView());

th.addTab(th.newTabSpec("tab1").setIndicator("精选表情").setContent(R.id.left));

th.addTab(th.newTabSpec("tab2").setIndicator("投稿表情").setContent(R.id.right));

580470a09c7d

ViewFlipper

ViewSwitcher只可以在两种布局中切换

ViewFlipper可以应用多种布局,且多出几个方法:

isFlipping 用来判断View切换是否正在进行

setFilpInterval 设置View之间切换的时间间隔

startFlipping 使用上面设置的时间间隔来开始切换所有的View,切换会循环进行

stopFlipping 停止View切换

viewFlipper = findViewById(R.id.flipper);

for(int i = 1; i < images.length; i++){

ImageView imageView = new ImageView(this);

imageView.setImageResource(images[i]);

viewFlipper.addView(imageView);

}

//然后

viewFlipper.setInAnimation(animations[0]);

viewFlipper.setOutAnimation(animations[2]);

viewFlipper.showNext();

//和

viewFlipper.setInAnimation(animations[1]);

viewFlipper.setOutAnimation(animations[3]);

viewFlipper.showPrevious();

580470a09c7d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值