问题
I have have a close button [x] in two of my xml (with floating dialog theme), which i am using viewPager with.
The close button is located at the top right hand corner of the two xml. However, as i swipe left or right, the button gets swipe to and fro too which looks really weird. Any idea to make the button stay fixed even when i swipe the pages?
Code from one of the page (both pages are similar):
<?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>
The 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>
回答1:
In the end what I did was to place the close button (btnClose) and page title (lblTitle) outside the viewPager as advised from @Luksprog . The close button is fixed when i applied onPageChangeListener for the page title so that the TextView would change the title accordingly to the page flipped! Codes :
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
来源:https://stackoverflow.com/questions/13281765/fixed-button-that-stays-even-when-viewpager-is-swiped