在ViewPager的页面上放置一个按钮

时间:2015-12-29 18:57:16

标签: android android-viewpager android-button

我正在使用ViewPager。我想在每个页面上放一个按钮,但这样做会填满整个页面。我想确定按钮的宽度和高度,但不知道如何。

这是我的代码:

寻呼机活动

public class PagerActivity extends Activity {

PagerContainer mContainer;

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

    mContainer = (PagerContainer) findViewById(R.id.pager_container);

    ViewPager pager = mContainer.getViewPager();
    PagerAdapter adapter = new MyPagerAdapter();
    pager.setAdapter(adapter);
    //Necessary or the pager will only have one extra page to show
    // make this at least however many pages you can see
    pager.setOffscreenPageLimit(adapter.getCount());
    //A little space between pages
    pager.setPageMargin(15);

    //If hardware acceleration is enabled, you should also remove
    // clipping on the pager for its children.
    pager.setClipChildren(false);




}

//Nothing special about this adapter, just throwing up colored views for demo
private class MyPagerAdapter extends PagerAdapter {

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        TextView view = new TextView(PagerActivity.this);
        view.setText("Item "+position);
        view.setGravity(Gravity.CENTER);
        view.setBackgroundColor(Color.argb(255, position * 50, position * 10, position * 50));
        view.setTextColor(Color.parseColor("#CA2C68"));

        Button buttonView = new Button(PagerActivity.this);
        buttonView.setBackgroundResource(R.drawable.button_states_azul);


        container.addView(buttonView);
        return buttonView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    @Override
    public int getCount() {
        return 5;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }
}

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="#FF7CB8"
    android:id="@+id/linear" >

    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="#CA2C68" 
        android:layout_gravity="bottom"/>
</LinearLayout>



<com.example.slidee.PagerContainer
    android:id="@+id/pager_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/linear"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/linear"
    android:background="#29C5FF" >

    <android.support.v4.view.ViewPager
        android:layout_width="234dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:background="#29C5FF" />
</com.example.slidee.PagerContainer>

This is the result. But I would determine the size of the button.

4 个答案:

答案 0 :(得分:1)

ViewPager期望包含的视图占用整个容器。 使用一些布局来包含按钮:

LinearLayout lay = new LinearLayout(PagerActivity.this);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
buttonView.setLayoutParams(lp);
lay.addView(buttonView);
container.addView(lay);

答案 1 :(得分:1)

另一种方法是创建一个新的xml文件并对其进行充气。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/viewpager_button"
android:background="@drawable/button_states_azul"/>
</LinearLayout>

并在instantiateItem

LayoutInflater inflater = MainActivity.this.getLayoutInflater();
        View pagerView = inflater.inflate(R.layout.viewpager_layout, null);
Button button = (Button) pagerView.findViewById(R.id.viewpager_button);
container.addView(button);

答案 2 :(得分:0)

您正在动态创建按钮,因此默认情况下它为宽度和高度执行match_parent。

添加以下行:

        buttonView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

答案 3 :(得分:0)

我就是这样做的,创建一个你要膨胀的xml布局。添加您想要的所有视图。然后在你的适配器类中,在instantiateItem覆盖中执行类似这样的操作。记得根据需要调整xml视图以适合父母的宽度和高度。

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.your_view, container, false);

    // get reference to your button over here if you have one
    Button b = (Button) view.findViewById(R.id.enter_button);


    container.addView(view);
    return view;
    }