昨天,只拿到电脑,别说,眼泪

http://joveth.github.io/funny/

1.选项卡的滑动效果

要知道。用这个选项卡就是想让它滑动起来,不然的话。我才不喜欢用它呢。

在让他滑动之前,先来说一下上一张 的问题。话说。依照设计器下载下来的包,替换到 我们的 res之后,我发现,tabhost的选项颜色没有变,在我尝试 了各种方法之后,最终,我tm放弃了,好吧。正好 找到了这个滑动效果的demo,还有选项卡的颜色切换效果。
话不多说 ,改进我们的东西吧。


1.改动我们的 acitivity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TabHost
android:id="@+id/tab_host"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/tab_widget_bg" /> <android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <!-- 隐藏 --> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" > <fragment
android:id="@+id/fragment_image"
android:name="com.jov.germany.frame.ImageFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <fragment
android:id="@+id/fragment_text"
android:name="com.jov.germany.frame.TextFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <fragment
android:id="@+id/fragment_both"
android:name="com.jov.germany.frame.BothFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
</TabHost> </LinearLayout>

这里面加入了viewpage。还有把FramLayout隐藏了

当中的布局文件:
res/color/tab_widget_text.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- 标签页tab 文字切换颜色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:color="@color/color_orange"/> <item android:color="#000"/> </selector>

value/color.xml中加入:


<color name="color_orange">#FF9224</color>

为啥是橘黄,我……假设你不喜欢,百度HTML颜色代码表,找一个你 自己的最爱


在来改动我们的 MainAcitivity.java


package com.jov.germany;

import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView; public class MainActivity extends FragmentActivity {
public static final String PAGE1_ID = "page1";
public static final String PAGE2_ID = "page2";
public static final String PAGE3_ID = "page3"; private TabHost tabHost; // TabHost
private List<View> views; // ViewPager内的View对象集合
private FragmentManager manager; // Activity管理器
private ViewPager pager; // ViewPager @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 初始化资源
pager = (ViewPager) findViewById(R.id.viewpager);
tabHost = (TabHost) findViewById(R.id.tab_host);
manager = getSupportFragmentManager();
views = new ArrayList<View>(); views.add(manager.findFragmentById(R.id.fragment_image).getView());
views.add(manager.findFragmentById(R.id.fragment_text).getView());
views.add(manager.findFragmentById(R.id.fragment_both).getView()); // 管理tabHost開始
tabHost.setup(); // 传一个空的内容给TabHost,不能用上面两个fragment
TabContentFactory factory = new TabContentFactory() {
@Override
public View createTabContent(String tag) {
return new View(MainActivity.this);
}
};
// tab1
TabSpec tabSpec = tabHost.newTabSpec(PAGE1_ID);
tabSpec.setIndicator(createTabView(R.string.fragment_image_str));
tabSpec.setContent(factory);
tabHost.addTab(tabSpec);
// tab2
TabSpec tabSpec2 = tabHost.newTabSpec(PAGE2_ID);
tabSpec2.setIndicator(createTabView(R.string.fragment_text_str));
tabSpec2.setContent(factory);
tabHost.addTab(tabSpec2);
// tab3
TabSpec tabSpec3 = tabHost.newTabSpec(PAGE3_ID);
tabSpec3.setIndicator(createTabView(R.string.fragment_both_str));
tabSpec3.setContent(factory);
tabHost.addTab(tabSpec3); tabHost.setCurrentTab(0);
// 管理tabHost结束 // 设置监听器和适配器
pager.setAdapter(new PageAdapter());
pager.setOnPageChangeListener(new PageChangeListener());
tabHost.setOnTabChangedListener(new TabChangeListener());
} /**
* PageView Adapter
*
* @author Administrator
*
*/
private class PageAdapter extends PagerAdapter {
@Override
public int getCount() {
return views.size();
} @Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
} @Override
public void destroyItem(ViewGroup view, int position, Object arg2) {
view.removeView(views.get(position));
} @Override
public Object instantiateItem(ViewGroup view, int position) {
try {
if (views.get(position).getParent() == null) {
view.addView(views.get(position));
} else {
((ViewGroup) views.get(position).getParent())
.removeView(views.get(position));
view.addView(views.get(position));
}
} catch (Exception e) {
e.printStackTrace();
}
return views.get(position);
}
} /**
* 标签页点击切换监听器
*
* @author Administrator
*
*/
private class TabChangeListener implements OnTabChangeListener {
@Override
public void onTabChanged(String tabId) {
if (PAGE1_ID.equals(tabId)) {
pager.setCurrentItem(0);
} else if (PAGE2_ID.equals(tabId)) {
pager.setCurrentItem(1);
} else if (PAGE3_ID.equals(tabId)) {
pager.setCurrentItem(2);
}
}
} /**
* ViewPager滑动切换监听器
*
* @author Administrator
*
*/
private class PageChangeListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
} @Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
} @Override
public void onPageSelected(int arg0) {
tabHost.setCurrentTab(arg0);
}
} /**
* 创建tab View
*
* @param string
* @return
*/
private View createTabView(int stringId) {
View tabView = getLayoutInflater().inflate(R.layout.tab, null);
TextView textView = (TextView) tabView.findViewById(R.id.tab_text);
textView.setText(stringId);
return tabView;
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

当中的layout/tab.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/state_tabs_bg" > <TextView
android:id="@+id/tab_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="11dp"
android:layout_gravity="center_horizontal"
android:textSize="18sp"
android:textColor="@color/tab_widget_text"/> </LinearLayout>

drawable/state_tabs_bg.xml

 

<?xml version="1.0" encoding="utf-8"?

>

<!-- tab每一个标签背景 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/tabs_selected_bg" /> <item android:drawable="@drawable/tabs_normal_bg"></item> </selector>

drawable/tabs_selected_bg.xml


<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item>
<shape>
<solid android:color="@color/color_orange" />
</shape>
</item>
<item android:bottom="5dp">
<shape>
<solid android:color="#eeeeee" />
</shape>
</item> </layer-list>

drawable/tabs_normal_bg.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<solid android:color="@color/color_orange" />
</shape>
</item>
<item android:bottom="1dp">
<shape>
<solid android:color="#eeeeee" />
</shape>
</item> </layer-list>

好至此的话 我们的代码算是ok了,可是当你执行的时候却发现跑不起来。为啥 呢???

且看一下我们的Frame里面,改动ImageFrame.java:

package com.jov.germany.frame;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.jov.germany.R; public class ImageFrame extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.image_frame, container, false);
}
}

你可能会认为没什么差别,看一下我们的Fragment引用的包,不一样哦。


好吧,最后MainAcitivity.java中的String内容自己 在string.xml里面自己加把


没有截滑动效果。

近期各种忙,更新的 有点慢,没办法啊



版权声明:本文博客原创文章,博客,未经同意,不得转载。

[android更新类的内容开发APP]四、项目布局的基本功能(继续)的更多相关文章

  1. Android系统移植与驱动开发--第四章

    第四章 源代码的下载和编译 一个android内核相当于4G,而一个Linux内个只有几百M,Linux内核相对于android内核来说实在是小巫见大巫.了解android源代码不一定要详细了解,只去 ...

  2. 【Android】13.0 UI开发(四)——列表控件RecyclerView的横向布局排列实现

    1.0 新建项目,由于ListView的局限性,RecyclerView是一种很好取代ListView的控件,可以灵活实现多种布局. 2.0 新建项目RecyclerviewTest,目录如下: 3. ...

  3. 深入浅出 - Android系统移植与平台开发(四)- Android启动流程

    作者:唐老师,华清远见嵌入式学院讲师. 一.Android init进程启动 还是从Linux的启动开始吧.Linux被bootloader加载到了内存之后,开始运行,在初始化完 Linux运行环境之 ...

  4. H5 + 开发App(分享功能)

    我们开发App有一个不可少的功能,就是分享功能.让用户将app分享到他的社交圈.比如微信 QQ 微博等等. 准备工作:我们要先去申请相关的权限, 这是传送门http://ask.dcloud.net. ...

  5. VS2013开发Windows服务项目

    这篇随笔里,我将介绍如何用VS2013开发Windows服务项目,实现的功能是定时发送电子邮件. 开发环境:VS2013,SQL Server2008,采用C#语言开发 步骤一:创建Windows服务 ...

  6. 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目

    系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 ... 基于. ...

  7. 基于.NetCore开发博客项目 StarBlog - (3) 模型设计

    系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...

  8. mui开发app之自定义事件以更新其他页内容

    我之前做过jquery mobile的开发,那还是前年的事情 在jquery mobile中,由于页面是存储在div[data-role=page]的dom中(jqmobile通过对data-role ...

  9. Cordova开发App入门之创建android项目

    Apache Cordova是一个开源的移动开发框架.允许使用标准的web技术-HTML5,CSS3和JavaScript做跨平台开发. 应用在每个平台的具体执行被封装了起来,并依靠符合标准的API绑 ...

随机推荐

  1. ASP.NET之Cookie(坑爹的Response.Cookies.Remove)

    原文:ASP.NET之Cookie(坑爹的Response.Cookies.Remove) 在web开发中Cookie是必不可少的 .NET自然也有一个强大的Cookie操作类,我们用起来也非常方便, ...

  2. HDU 4982 Goffi and Squary Partition(推理)

    HDU 4982 Goffi and Squary Partition 思路:直接从全然平方数往下找,然后推断是否能构造出该全然平方数,假设能够就是yes,假设都不行就是no.注意构造时候的推断,因为 ...

  3. abstract修改方法

    abstract这种方法修饰,主要用在抽象类和抽象方法. 抽象的类是不可实例化的比如 public abstract class Test{ } 他能够含有抽象的方法 public abstract ...

  4. CSDN挑战编程——《金色十月线上编程比赛第二题:解密》

    金色十月线上编程比赛第二题:解密 题目详情: 小强是一名学生, 同一时候他也是一个黑客. 考试结束后不久.他吃惊的发现自己的高等数学科目竟然挂了,于是他果断入侵了学校教务部站点. 在入侵的过程中.他发 ...

  5. play framework2.5.

    play framework2 的学习笔记 https://github.com/playframework/playframework https://github.com/playframewor ...

  6. 认识Underscore

    Underscore一个JavaScript实用库,提供了一整套函数式编程的实用功能,但是没有扩展任何JavaScript内置对象.它弥补了部分jQuery没有实现的功能,同时又是Backbone.j ...

  7. 集合hashCode()方法和equals()办法

    1.哈希码:         Object中的HashCode方法会返回该对象的的内存真实地址的整数化表示,这个形象的不是真正抵制的整数值就是哈希码. 2.利用哈希码向集合中插入数据的顺序?     ...

  8. 全新E:网站不是之前排名浮动 相比于竞争对手究竟缺少了什么?

    这几天有非常多朋友问新辰,为什么站点排名掉了?为什么被人家逆袭反超了?当然,这无疑与你站点的内容.外链和用户体验有非常大关系,只是.新辰在此觉得,还须要多研究一下竞争对手的站点,做到:人无我有.人有我 ...

  9. liunx tomcat多站点配置

    <Host name="nav" debug="0" appBase="webapps"  unpackWARs="true ...

  10. VirtualBox创建虚拟电脑、执行Genymotion模拟器报错

    当安装完Genynition关于Android应用的调试模拟器之后,在Genymotion执行的平台virtualBox:VirtualBox创建虚拟电脑.执行Genymotion模拟器报错: 错误卖 ...