本文来自网易云社区

作者:孙有军

前言

这里主要记录几个TV问题的解决方案,如果对这个不感兴趣的其实就不用往下看了。

这几天有一个需求就是要求出一个TV版本的app,之前没有具体的了解Tv版的app有手机端的app到底有什么区别,因此就做了一下研究,写了些Demo,在做的过程中确实出现了好几个问题。一开始碰到这些问题时,浅尝辄止的试了试,发现很多都没有解决方案,本着外事问google的,search了一把,也没有结果,可能是TV做的人比较少,网上搜出来的都是照着谷歌官方的样例实现了一把而已,因此就仔细的研究了一下这些问题,这里把解决这些问题的方案描述出来,希望其他人能少走弯路,如果有更好的解决方案也希望大家探讨分享。

开发过程

虽然google官方写的是手机app不用做太多改动就可以运行在Tv上,但是终究两种还是有部分区别的,这里还是要针对TV版做部分设置。
首先是配置文件的改动,需要在AndroidManifest中配置如下属性:

<uses-featureandroid:name="android.hardware.touchscreen"android:required="false"/>
<uses-featureandroid:name="android.software.leanback"android:required="true"/>复制代码

同时还需要配置一个action为android.intent.action.MAIN,category为android.intent.category.LEANBACK_LAUNCHER的Activity,类似如下:

<activityandroid:name="im.yixin.home.HomeActivity"android:label="@string/app_name"android:screenOrientation="landscape"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LEANBACK_LAUNCHER"/></intent-filter>
</activity>复制代码

如果记不住上面需要配置的内容其实也没有关系,可以新创建一个TV工程,默认创建的TV工程就已经包含了上述的配置,并且该工程就相当于一个demo了,是可以直接运行的一个工程,里面包含了Tv开发的很多控件,如果你要学习这也是很好的学习资料了,其实后续的内容也是根据这里的内容进行参照学习的。

这里附带一句,Android的sdk中的samples中的tv样例程序直接导入是运行不起来的,需要修改很多东西,但是实质内容与新创建的工程没有什么区别,因此也可以不用导入样例程序进行学习了。

根据前面的样例图,主界面配置页面如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/global_bg"android:orientation="vertical"android:paddingLeft="42dp"android:paddingRight="42dp"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="@dimen/gap_86_dp"android:clickable="true"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:drawableLeft="@drawable/tv_logo"android:drawablePadding="@dimen/gap_8_dp"android:gravity="center"android:text="@string/itv_name"android:textColor="@color/white"android:textSize="@dimen/text_size_20"/><TextViewandroid:id="@+id/settings_tab"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_alignParentRight="true"android:layout_marginRight="@dimen/gap_45_dp"android:background="@drawable/navigation_tab_bar_selector"android:focusable="true"android:gravity="center"android:text="@string/setting"android:textColor="@color/navigation_text_selector"android:textSize="@dimen/text_size_20"/><TextViewandroid:id="@+id/contact_tab"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginRight="@dimen/gap_45_dp"android:layout_toLeftOf="@id/settings_tab"android:background="@drawable/navigation_tab_bar_selector"android:focusable="true"android:gravity="center"android:text="@string/contact"android:textColor="@color/navigation_text_selector"android:textSize="@dimen/text_size_20"/><TextViewandroid:id="@+id/dial_tab"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginRight="@dimen/gap_65_dp"android:layout_toLeftOf="@id/contact_tab"android:background="@drawable/navigation_tab_bar_selector"android:focusable="true"android:gravity="center"android:text="@string/dial"android:textColor="@color/navigation_text_selector"android:textSize="@dimen/text_size_20"/></RelativeLayout><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:layout_marginBottom="@dimen/gap_50_dp"android:background="@color/gray1"/><FrameLayoutandroid:id="@+id/tab_container"android:layout_width="match_parent"android:layout_height="match_parent"></FrameLayout>
</LinearLayout>复制代码

界面的代码如下:

public class HomeActivity extends Activity implements View.OnClickListener {public static void start(Context context) {Intent intent = new Intent(context, HomeActivity.class);context.startActivity(intent);}private static final String[] TAGS = {"dial", "contact", "my"};private FragmentManager manager;private int showTabIndex = -1;private TextView dialTab;private TextView contactTab;private TextView myTab;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_home);findViews();setViewsListener();init();selectTab(0);}private void findViews() {dialTab = (TextView) findViewById(R.id.dial_tab);contactTab = (TextView) findViewById(R.id.contact_tab);myTab = (TextView) findViewById(R.id.settings_tab);}private void setViewsListener() {dialTab.setOnClickListener(this);contactTab.setOnClickListener(this);myTab.setOnClickListener(this);}private void init() {manager = getFragmentManager();}private void selectTab(int index) {if (index == showTabIndex) {return;}dialTab.setSelected(index == 0);contactTab.setSelected(index == 1);myTab.setSelected(index == 2);FragmentTransaction transaction = manager.beginTransaction();hideFragment(showTabIndex, transaction);showTabIndex = index;showFragment(showTabIndex, transaction);transaction.commit();}private void hideFragment(int tabIndex, FragmentTransaction transaction) {Fragment fragment = getFragmentByIndex(tabIndex);if (fragment != null) {transaction.hide(fragment);}}private Fragment getFragmentByIndex(int index) {if (index >= 0 && index < TAGS.length) {return manager.findFragmentByTag(TAGS[index]);}return null;}private void showFragment(int tabIndex, FragmentTransaction transaction) {Fragment fragment = getFragmentByIndex(tabIndex);if (fragment == null) {switch (tabIndex) {case 0:fragment = new DialFragment();break;case 1:/*  fragment = new ContactFragment();*/fragment = new VerticalGridFragment();break;case 2:fragment = new MyFragment();break;}transaction.add(R.id.tab_container, fragment, TAGS[tabIndex]);//transaction.addToBackStack(TAGS[tabIndex]);} else {transaction.show(fragment);}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.dial_tab:selectTab(0);return;case R.id.contact_tab:selectTab(1);return;case R.id.settings_tab:selectTab(2);//                VerticalGridActivity.start(this);return;}}}复制代码

该界面主要采用Fragment来实现三个界面,分别为拨号页面,好友,设置界面,其中拨号界面又包含两个子的Fragment,我们来继续看看拨号界面与好友界面,设置界面是一个充数的界面啥都没有做。

网易云免费体验馆,0成本体验20+款云产品!

更多网易研发、产品、运营经验分享请访问网易云社区

相关文章:
【推荐】 常用数据清洗方法大盘点

Android TV 开发 (1)相关推荐

  1. Android TV开发总结(三)构建一个TV app的焦点控制及遇到的坑

    原文:Android TV开发总结(三)构建一个TV app的焦点控制及遇到的坑 版权声明:我已委托"维权骑士"(rightknights.com)为我的文章进行维权行动.转载务必 ...

  2. 【Android TV 开发】焦点处理 ( 父容器与子组件焦点获取关系处理 | 不同电视设备上的兼容问题 | 触摸获取焦点 | 按键获取焦点 )

    Android TV 开发系列文章目录 [Android TV 开发]安卓电视调试 ( 开启网络远程调试 ) [Android TV 开发]焦点处理 ( 父容器与子组件焦点获取关系处理 | 不同电视设 ...

  3. android tv 菜单键,Android TV开发总结(三)构建一个TV app的焦点控制及遇到的坑

    前言:关于<TV Metro界面(仿泰捷视频TV版)源码解析>由于都是相关代码,就不发公众号了,有兴趣的可以看链接:http://blog.csdn.net/hejjunlin/artic ...

  4. android tv 云播放器,Android TV开发总结(六)构建一个TV app的直播节目实例

    近年来,Android TV的迅速发展,传统的有线电视受到较大的冲击,在TV上用户同样也可以看到各个有线电视的直播频道,相对于手机,这种直播节目,体验效果更佳,尤其是一样赛事节目,大屏幕看得才够痛快, ...

  5. android 仿 tv 菜单,Android TV 开发之仿泰捷视频最新 TV 版 Metro UI 效果

    Some Android TV related Sample 更多TV相关,欢迎关注公众号: Android TV开发交流群:135622564 1.Imitation of tai jie late ...

  6. android 按键分析,Android TV开发按键与焦点深入分析(四)

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 前面三篇都是从源码的角度分析按键事件.焦点变换的原理,作为应用层的开发者, 分析源码都是带着实际的开发困惑的,要不然谁没 ...

  7. Android TV开发:APP安装、ICON图标问题

    使用AndroidX版本的Android Studio开发的面向TV的APK,安装后,在电视默认主屏没有显示该APP的ICON,是怎么回事? 一开始没有注意到电视的Android版本,安装APK时出现 ...

  8. 聊聊真实的 Android TV 开发技术栈

    智能电视越来越普及了,华为说四月发布智能电视跳票了,一加也说今后要布局智能电视,在智能电视方向,小米已经算是先驱了.但是还有不少开发把智能电视简单的理解成手机屏幕的放大,其实这两者并不一样. 一.序 ...

  9. Android TV 开发有关PopupWindow的KeyListener(手机也能用)

    转载请标明原地址:Android TV 开发有关PopupWindow的KeyListener(手机也能用)_高磊的专栏-CSDN博客 现在这个公司主要是做智能电视视频方面.有硬件电视盒子,APP开发 ...

最新文章

  1. python怎么建文件dome_Python专题(四) 如何制作一个demo给老板看
  2. oracle区号,Oracle 存儲過程
  3. Arthas 初探--安装初步适用
  4. Android开发 ---多线程操作:Handler对象,消息队列,异步任务下载
  5. 牛客网暑期ACM多校训练营(第二场)J farm (二维树状数组)
  6. hdu4336-Card Collector【min-max容斥,期望概率】
  7. LeetCode 09. 回文数
  8. 作者:徐优俊(1990-),男,北京大学前沿交叉学科研究院博士生。
  9. 电力系统非线性控制_什么是谐波?电力系统谐波怎么产生的?老司机给你科普一下!...
  10. Eclipse 常用快捷键收集
  11. ctfshow-萌新-web9( 利用命令执行漏洞读取网站敏感文件)
  12. 历法 —— 十二地支与二十八星宿
  13. 打开*.HLP时,系统提示*.hlp是为此Windows版本不支持的语言创建的的解决方法!
  14. python 实现数据化大屏_基于Python实现交互式数据可视化的工具(用于Web)
  15. 小刘同学的CMOS模拟集成电路学习小记(不停更新)
  16. Activiti学习记录 Activiti初始化数据库、Activiti6增加表注释字段注释
  17. 美团圈圈是什么?美团圈圈介绍,美团圈圈是什么平台?
  18. 预训练模型微调 | 一文带你了解Adapter Tuning
  19. SSH Tunneling
  20. selenium使用代理IP

热门文章

  1. python冒泡循环示例_Python循环示例–循环在python中
  2. mongodb find_MongoDB find()
  3. 关于appium中的Multiaction的用法
  4. 时间序列数据的存储和计算 - 开源时序数据库解析(一)
  5. 法国政府正考虑采用开源软件
  6. DELL-S4810恢复出厂配置
  7. Android 内存监测工具 DDMS -- Heap
  8. 互联网企业招聘零分考生 扇了谁的耳光?
  9. 剑指offer——面试题60:把二叉树打印成多行
  10. Leetcode 106.从中序与后序遍历序列重建二叉树