您的位置:首页 > 其它

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布局可以覆盖控件!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: