2012-11-08 41 views
1

固定按鈕我有一個關閉按鈕[x]在我的兩個xml(浮動對話框主題),我使用viewPager。固定按鈕,即使viewPager被刷卡

關閉按鈕位於兩個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:minWidth="350dp" 
    android:minHeight="700dp" 
    android:background="#CECECE" 
    android:id="@+id/dialogLayout" 
    android:orientation="vertical"> 

<TableRow 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

<TextView 
    android:id="@+id/lblInfo" 
    android:layout_marginLeft="10dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="-6dp" 
    android:text="Information" 
    android:textStyle="bold" 
    android:textColor="#000" 
    android:textSize="40dp" /> 

<ImageButton 
    android:id="@+id/btnClose" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="320dp" 
    android:layout_marginTop="-4dp" 
    android:layout_marginRight="-4dp" 
    android:background="#0000" 
    android:src="@drawable/close" /> 

</TableRow> 

<TextView 
    android:id="@+id/lblInfo" 
    android:layout_width="wrap_content" 
    android:layout_height="400dp" 
    android:layout_alignParentRight="true" 
    android:layout_marginLeft="10dp" 
    android:textColor="#000" 
    android:textSize="17dp" /> 



</LinearLayout> 

的viewPager XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="612dp" 
    android:layout_height="700dp"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewPager" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
+0

從佈局中發佈xml – dymmeh

回答

2

到底是什麼我所做的就是將關閉按鈕(btnClose)和頁面標題(lblTitle)從@Luksprog建議的viewPager外部。當我爲頁面標題應用onPageChangeListener時,關閉按鈕是固定的,這樣TextView會相應地將標題更改爲翻轉的頁面!代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.viewpagerbase); 

    //add onClickListener for the btnClose 
    ImageButton btnClose = (ImageButton)findViewById(R.id.btnClose); 
    btnClose.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      onBackPressed(); 
     } 
    }); 


    //attach the viewPage adapter 
    MyPagerAdapter adapter = new MyPagerAdapter(); 
    ViewPager myPager = (ViewPager) findViewById(R.id.viewPager); 
    myPager.setAdapter(adapter); 
    myPager.setCurrentItem(0); 
    //set listener for page change 
    myPager.setOnPageChangeListener(this); 

} //end of onCreate