Android(安卓)TV自定义通用标题栏(组合控件)
16lz
2022-03-14
一,天气,图片,网络状态,时间自动刷新,广告轮播。
package spa.nabo.com.nabospa.view;import android.content.Context;import android.os.Handler;import android.os.Message;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import com.bumptech.glide.Glide;import java.text.SimpleDateFormat;import java.util.Date;import java.util.TimeZone;import java.util.logging.LogRecord;import spa.nabo.com.nabospa.R;/** * Created by cwj on 2018/3/22 0019. * 描述: 自定义标题栏。 */public class title extends LinearLayout { ImageView logoIv; //logo TextView addrTv; //地址 ImageView weatherIv;//天气 TextView tempTv; //温度 TextView adveTv; //广告。 ImageView networkIv;//网络状态。 TextView dateTv; //日期 public Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); //这里刷新UI dateTv.setText(netTime()); } }; public title(Context context) { super(context); } public title(Context context, @Nullable AttributeSet attrs) { super(context, attrs); //加载布局。 LayoutInflater.from(context).inflate(R.layout.title, this); //获取控件 logoIv = findViewById(R.id.logo_iv); addrTv = findViewById(R.id.addr_tv); weatherIv = findViewById(R.id.weather_iv); tempTv = findViewById(R.id.temp_tv); adveTv = findViewById(R.id.adve_tv); networkIv = findViewById(R.id.network_iv); dateTv = findViewById(R.id.date_tv); adveTv.setSelected(true); } public title(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public title(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } /** * 设置logo * * @param img String */ public void setLogoIv(String img) { Glide.with(getContext()).load(img).into(logoIv); } /** * 设置logo * * @param img int */ public void setLogoIv(int img) { Glide.with(getContext()) .load(img) .into(logoIv); } /** * 设置地址 * * @param addr String */ public void setAddrTv(String addr) { addrTv.setText(addr); } /** * 设置广告 轮播 * * @param adve */ public void setAdveTv(String adve) { adveTv.setText(adve); } /** * 设置天气图标 * * @param weathType 天气类型。 */ public void setWeatherIv(int weathType) { switch (weathType) { //TODO: 根据后台来的数据 来判断对应的天气图标。。未完持续。。。。 case 1:// Glide.with(getContext()).load().into(weatherIv); break; case 2: break; case 3: break; case 4: default: } } /** * 设置温度。 * * @param temp */ public void setTempTv(String temp) { tempTv.setText(temp); } /** * 根据联网状态设置图标 * * @param netWorkType 网络图标 * 这里 根据 main 页 发送的广播 EvenBus 来做判断。。 */ public void setNetworkIv(int netWorkType) { //TODO: 网络判断。 switch (netWorkType) { case 0:// 没有网络连接 //改变图片。 Glide.with(getContext()) .load(R.drawable.ic_network_n) .into(networkIv); break; case 1:// 是有线网络。 Glide.with(getContext()) .load(R.drawable.ic_network) .into(networkIv); break; case 2:// 是无线网络。 Glide.with(getContext()) .load(R.drawable.ic_lianjie_true) .into(networkIv); break; default: break; } } /** * 设置时间 日期。 * * @param */ public void setDateTv() { new TimeThread().start(); // TODO: 2018/3/22 0022 根据后台 实际做出 整改。 } //开一个线程继承Thread public class TimeThread extends Thread { //重写run方法 @Override public void run() { super.run(); // do-while 一 什么什么 。。 do { try { //每隔一秒 发送一次消息 Thread.sleep(1000); Message msg = new Message(); //消息内容 为MSG_ONE msg.what = 1; //发送 handler.sendMessage(msg); } catch (InterruptedException e) { e.printStackTrace(); } } while (true); } }//获取网络时间 public String netTime() { SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dff.setTimeZone(TimeZone.getTimeZone("GMT+08")); String ee = dff.format(new Date()); return ee; }}
//监听网络状态的广播
package spa.nabo.com.nabospa.receiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.net.ConnectivityManager;import android.net.NetworkInfo;import org.greenrobot.eventbus.EventBus;import spa.nabo.com.nabospa.evenbus.NetWorkState;/** * Created by Administrator on 2018/1/23 0023. * 监听网络状态 广播接收器。 */public class NetWorkStateReceiver extends BroadcastReceiver { public static int state = 0;//0 是没有网络连接。1 是有线网络。2 是无线网络。 @Override public void onReceive(Context context, Intent intent) { ConnectivityManager manager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (manager!=null){ NetworkInfo info = manager.getActiveNetworkInfo(); boolean up = false; if(info != null && info.isConnected()){ up = true; state = 1; EventBus.getDefault().post(new NetWorkState(state)); }else{ state = 0; EventBus.getDefault().post(new NetWorkState(state)); } } }}
在Activity方法里面使用
public static void setTites(spa.nabo.com.nabospa.view.title title) {// title.setTempTv();//温度 title.setAddrTv(city);//地址// title.setNetworkIv();//网络状态 title.setLogoIv(logoPath);//看路径 title.setDateTv();//shijian title.setAdveTv(msg);//轮播广告 title.setTempTv(Config.szuoweihao + ""); Log.e("city" + city + "", "TIME:" + times + "--logopath:" + logoPath + "---msg:" + msg);// title.setWeatherIv();//天气 }
记个笔记。留个爪。。
更多相关文章
- Android(安卓)布局
- android之WebView的使用
- android view画图笔记-1
- Android滚动文字效果
- android > 广播监听电话状态
- android 按纽状态配置文件
- android之获得当前连接wifi的名字
- Android(安卓)实现旋转键盘的例子
- 基于Android的邮件收发