将按钮添加到ViewPager [英] Add button to ViewPager

查看:70
本文介绍了将按钮添加到ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewPager,显示了一些布局.我需要添加两个按钮,并以编程方式在所有布局中显示相同的按钮.查看器是带有ViewPager的简单XML.我该怎么办?

I have a ViewPager showing some layouts. I need to add two buttons and show the same buttons in all the layouts programmatically. The viewer is a simple XML with a ViewPager. How can I do this?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // View layout = (View) findViewById(R.id.pager);
    //reply.setLayoutParams(new LayoutParams());

    // Inicialización
    ArrayList<Integer> inte = new ArrayList<Integer>();
    viewPager = (ViewPager) findViewById(R.id.pager);
    //ArrayList<Integer> pageNumber = new ArrayList<Integer>();

    actionBar = getActionBar();
    for(int i = 0; i < 39; i++) {
        inte.add(i%3);
    }
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), inte);
    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Agrega tabs
    for(Integer i =0; i<10; i++){
        JSONObject aaa = new JSONObject();
        try {
            aaa.put("name", "Página "+(i+1));
            aaa.put("type", i%3);
            System.out.println("name" + " Página ");
            inte.add(i%3);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        actionBar.addTab(actionBar.newTab().setText("Página "+(i+1)).setTabListener(this));
    }

推荐答案

为什么不只在XML中定义它们,以便它们显示在ViewPager下方的所有片段中?例如:

Why not just define them in XML so they appear below your ViewPager for all fragments? For example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/palette_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
android:orientation="vertical" >


<com.yourdomain.yourapp.YourCustomViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="10">
</com.yourdomain.yourapp.YourCustomViewPager>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_marginTop="2dp"
    android:orientation="horizontal" >

   //Your buttons here.

</LinearLayout>

这篇关于将按钮添加到ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆