java - 以编程方式将按钮添加到 fragment

标签 java android xml

我创建了一个带有可滚动选项卡的项目。每个选项卡都由一个 fragment 声明。现在我想要 f.e. Fragment1 有 8 个按钮,Fragment2 有 4 个按钮,依此类推。我想要有行,每行最多包含三个按钮,所以如果我生成 4 个按钮,则应该有一行有 3 个按钮,一行下面有 1 个按钮。这就是我制作可滚动标签的方式:

我的 Main_Activity.java

public class MainActivity extends FragmentActivity {
    ViewPager viewPager = null;
    SoundPlayer sp = new SoundPlayer();
   protected void onCreate(Bundle savedInstanceStats)
   {
       super.onCreate(savedInstanceStats);
       setContentView(R.layout.homescreen);
       viewPager = (ViewPager) findViewById(R.id.pager);
       FragmentManager fragmentManager = getSupportFragmentManager();
       viewPager.setAdapter(new MyAdapter(fragmentManager));
   }
}

class MyAdapter extends FragmentPagerAdapter
{

    public MyAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        if(position == 0)
            fragment = new FragmentDominik();
        else if(position == 1)
            fragment = new FragmentTobias();
        else if(position == 2)
            fragment = new FragmentTom();
        else if(position == 3)
            fragment = new FragmentNikolas();
        else if(position == 4)
            fragment = new FragmentGroups();
        else if(position == 5)
            fragment = new FragmentOthers();
        else if(position == 6)
            fragment = new FragmentFavorites();
        return fragment;
    }

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

    public CharSequence getPageTitle(int position)
    {
        String title = new String();
        if(position == 0)
            return "Dominik";
        else if(position == 1)
            return "Tobias";
        else if(position == 2)
            return "Tom";
        else if(position == 3)
            return "Nikolas";
        else if(position == 4)
            return "Groups";
        else if(position == 5)
            return "Others";
        else if(position == 6)
            return "Favorites";
        return null;
    }
}

我的主屏幕.xml

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/pager"
    android:layout_height="match_parent">

    <android.support.v4.view.PagerTitleStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:layout_gravity="top"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:background="#4052b5"
        android:focusableInTouchMode="true"
        android:textColor="#ffffff"/>
</android.support.v4.view.ViewPager>

一个例子Fragment.java

public class FragmentTobias extends Fragment {

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.tobias, container, false);
        }
    }

一个示例 Fragment.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"
    android:background="#d1d1d1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView3"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Tobias"
        android:id="@+id/textView7"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

所以现在如果我想在 tobias.xml 中生成 8 个按钮,在 FragmentTobias.java 中以语法方式生成,我该怎么做?我是创建 Android 应用程序的新手,因此非常感谢您的帮助。

编辑1:

LinearLayout linearLayout = new LinearLayout(getActivity());
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(params1);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL); //or VERTICAL

        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        Button button = new Button(getActivity());
        button.setLayoutParams(params2);

        Button button2 = new Button(getActivity());
        button2.setLayoutParams(params2);

        //like this, add all buttons and other views
        //you can use a loop for adding multiple similar views

        linearLayout.addView(button);
        linearLayout.addView(button2);
        container.addView(linearLayout);

最佳答案

您可以动态地将按钮添加到 ViewGroup 并使用它来扩充您的 View 。试试下面的代码,

public class FragmentTobias extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        LinearLayout linearLayout = new LinearLayout(getActivity());
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL); //or VERTICAL

        LayoutParams buttonParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        Button button = new Button(getActivity());
        button.setLayoutParams(buttonParams);

        Button button2 = new Button(getActivity());
        button2.setLayoutParams(buttonParams);

        linearLayout.addView(button);
        linearLayout.addView(button2);

        //like this, add all buttons and other views
        //you can use a loop for adding multiple similar views

        container.addView(linearLayout);
        View view = inflater.inflate(R.layout.tobias, container, false);

        return view;
    }
}

关于java - 以编程方式将按钮添加到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34963818/

相关文章:

java - 单击按钮时屏幕响应

java - RxJava - 何时将 Observable 与创建方法一起使用

java - NetBeans - 如何在 JFrame 中显示所有 Swing 组件?

android - 与 passthrough 相比,binderised hidl 的优势是什么?

android - 使用适用于 Android 的 google places api 网络服务搜索附近的特定地点

java - 如何使用节点名称和特定属性值获取特定节点及其子节点?

java - 从 tomcat 服务器中的 war 文件运行 java 类

java - 单击时按钮旋转

java - Android,按钮根本不起作用

xml - Magento 使用 soap v2 创建类别