button = (Button) views.get(2).findViewById(R.id.start_btn);

主布局

<?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布局可以覆盖控件!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值