button = (Button) views.get(2).findViewById(R.id.start_btn);
2015-09-28 20:58
507 查看
主布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ll"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv1"
android:src="@drawable/login_point_selected"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv2"
android:src="@drawable/login_point"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv3"
android:src="@drawable/login_point"/>
</LinearLayout>
</RelativeLayout>
代码
public class Guide extends Activity implements OnPageChangeListener,OnClickListener{
private ViewPager vp;
private Button button;
private ViewPagerAdapter vpAdapter;
private List<View> views;
private ImageView [] dots;
private int[] ids={R.id.iv1,R.id.iv2,R.id.iv3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide);
inintView();
initDots();
}
private void inintView(){
LayoutInflater inflater = LayoutInflater.from(this);
views = new ArrayList<View>();
views.add(inflater.inflate(R.layout.one, null));
views.add(inflater.inflate(R.layout.two, null));
views.add(inflater.inflate(R.layout.three, null));
vpAdapter = new ViewPagerAdapter(views, this);
vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(vpAdapter);
vp.setOnPageChangeListener(this);
button = (Button) views.get(2).findViewById(R.id.start_btn);
button.setOnClickListener(this);
}
private void initDots(){
dots = new ImageView[views.size()];
for (int i = 0; i < views.size(); i++) {
dots[i]=(ImageView) findViewById(ids[i]);
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < views.size(); i++) {
if (arg0 == i) {
dots[i].setImageResource(R.drawable.login_point_selected);
}else{
dots[i].setImageResource(R.drawable.login_point);
}
}
}
@Override
public void onClick(View v) {
startActivity(new Intent(Guide.this,MainActivity.class));
}
}
其中view中加载了三个布局
three.xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg3"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/start_btn"
android:text="JINRU"
/>
</LinearLayout>
</RelativeLayout>
获取three布局中的button实例
button = (Button) views.get(2).findViewById(R.id.start_btn);
直接写findViewById(R.id.start_btn)出错
RelativeLayout布局可以覆盖控件!!!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ll"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv1"
android:src="@drawable/login_point_selected"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv2"
android:src="@drawable/login_point"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv3"
android:src="@drawable/login_point"/>
</LinearLayout>
</RelativeLayout>
代码
public class Guide extends Activity implements OnPageChangeListener,OnClickListener{
private ViewPager vp;
private Button button;
private ViewPagerAdapter vpAdapter;
private List<View> views;
private ImageView [] dots;
private int[] ids={R.id.iv1,R.id.iv2,R.id.iv3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide);
inintView();
initDots();
}
private void inintView(){
LayoutInflater inflater = LayoutInflater.from(this);
views = new ArrayList<View>();
views.add(inflater.inflate(R.layout.one, null));
views.add(inflater.inflate(R.layout.two, null));
views.add(inflater.inflate(R.layout.three, null));
vpAdapter = new ViewPagerAdapter(views, this);
vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(vpAdapter);
vp.setOnPageChangeListener(this);
button = (Button) views.get(2).findViewById(R.id.start_btn);
button.setOnClickListener(this);
}
private void initDots(){
dots = new ImageView[views.size()];
for (int i = 0; i < views.size(); i++) {
dots[i]=(ImageView) findViewById(ids[i]);
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < views.size(); i++) {
if (arg0 == i) {
dots[i].setImageResource(R.drawable.login_point_selected);
}else{
dots[i].setImageResource(R.drawable.login_point);
}
}
}
@Override
public void onClick(View v) {
startActivity(new Intent(Guide.this,MainActivity.class));
}
}
其中view中加载了三个布局
three.xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg3"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/start_btn"
android:text="JINRU"
/>
</LinearLayout>
</RelativeLayout>
获取three布局中的button实例
button = (Button) views.get(2).findViewById(R.id.start_btn);
直接写findViewById(R.id.start_btn)出错
RelativeLayout布局可以覆盖控件!!!
相关文章推荐
- 输入的数转化为二进制序列,并统计序列中1的个数
- 关于最大匹配,最小点覆盖,最少路径覆盖和最大独立集的总结
- 计算器程序
- kafka0.8.2.1常用命令
- TeaScoreFrame
- CYC- IOS 常用的一些小东西
- 第八章 网络配置管理
- docker与Hadoop
- Linux伙伴系统算法--防止内存碎片的产生
- 将一个字符串插入到另一个字符串的某个位置
- iOS界面编程-UIImageView
- 构造和析构函数对比
- 部署完openstack后的网络配置(二)
- 一篇不错的hadoop介绍文章
- openstack 和 Hadoop
- druid StatViewServlet配置 内置web页面
- Android 应用程序退出后不在运行列表中显示的方法
- BZOJ 2741【FOTILE模拟赛】L 分块+可持久化trie
- 服务器开发日常9-28-2015
- SysManagerServiceImpl