java - 将按钮添加到 ViewPager

标签 java android eclipse view

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

@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 中定义它们,以便它们显示在所有 fragment 的 ViewPager 下方?例如:

<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>

关于java - 将按钮添加到 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27234817/

相关文章:

java - hadoop 使用类名提交作业,为什么需要 job.setJarByClass()?

java - 错误: Failed to resolve ExpandableLayout

android - 在 react native 中发布 apk 时出现“react-native-gesture-handler”错误

java - 如何将 jingle_peerconnection_so.so 文件加载到 eclipse 中的 android 项目?

java - Eclipse 如何知道我没有调用某个对象的某些方法?

c# - C# TcpListener 和 Java ServerSocket 的区别

java - 如何在 Thymeleaf 3.0.5 中从 DB 读取 Thymeleaf 模板?

java - eclipse RCP : clonePerspective returns null

java - DB Microsoft Excel 驱动程序

android - 使用 AsyncTask 启动 Activity