1

所以,我正在尝试制作一列按钮。在谷歌搜索解决方案后,我能找到的最好的方法是将它放入 LinearLayout 或类似的东西中,但我的问题是我正在使用片段(特别是导航类型“Tabs + Swipe”)并且它不使用据我所知的布局。(嗯,好的,确实如此,但不是在页面查看器上)为了获得一个按钮,我修改了生成的代码看起来像

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
        final TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);

        final Button button = (Button) rootView.findViewById(R.id.button1);
        button.setText("Btn Text"); 
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                                    // Perform action on click
            }
        });

在咨询了 Reddit/LearnProgramming 的人之后,我被告知我应该以编程方式制作按钮并更改布局参数,而不是为每个按钮制作 XML。我有点困惑,失去了他们的帮助,所以我回到谷歌。之后,我的代码现在看起来像:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
        TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));

        for (int i=0;i<4;i++){
            final Button newButton = new Button(getActivity());
            newButton.setText("This is some text for"+i);
            newButton.setTag(i);
            arrList.add(newButton);
           // ((ViewGroup) rootView).addView(newButton); // Works for a single button

            ((ViewGroup) rootView).addView(newButton, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            newButton.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  newButton.setText("I changed it.");
              }
          });
            }

        return rootView;
    }

这会产生一个默认的 textview 编号,说明我在哪个页面上以及四个重叠的按钮。我知道它重叠并且它们都被显示,因为我可以看到数字,1-4 重叠。我对编程并不陌生,但我对 android 编程相当陌生。

我希望发生的是将按钮连续添加到彼此下方。谁能把我推向正确的方向?我尽可能清楚地说明了这一点,但是如果您需要更多信息,我很乐意添加。提前致谢。

编辑:这是 fragment_main_dummy.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity$DummySectionFragment" >

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/MyLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/section_label"
        android:layout_marginTop="16dp"
        android:layout_toRightOf="@+id/section_label"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

所以它是相对的,但我在代码中的任何地方都没有看到对 RelativeLayout 的引用,所以我被引导相信它没有被使用?另外,我尝试在 xml 中添加一个 LinearLayout 作为子项,但我没有对它做任何事情。

4

1 回答 1

1

尝试这个:

for (int i=0;i<4;i++){
   final Button newButton = new Button(getActivity());
   newButton.setText("This is some text for"+i);
   newButton.setTag(i);
   arrList.add(newButton);
   newButton.setId(1000 + i);
   RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);   
   rlp.addRule(RelativeLayout.BELOW, i == 0 ? R.id.MyLayout : newButton.getId() - 1);
   newButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             newButton.setText("I changed it.");
        }
   });
   ((RelativeLayout) rootView).addView(newButton, rlp);
}
于 2013-04-30T18:15:07.673 回答