【问题标题】:Making a column of buttons in the fragment's layout在片段的布局中制作一列按钮
【发布时间】:2013-04-30 15:57:26
【问题描述】:

所以,我正在尝试制作一列按钮。在谷歌搜索解决方案后,我能找到的最好的方法是将它放入 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 作为子项,但我没有对它做任何事情。

【问题讨论】:

  • 如果您不提及添加Buttons 的布局是什么,那么没有人可以帮助您。如果rootViewRelativeLayoutFrameLayout,则按钮重叠是正常的,因为您没有特别设置LayoutParams
  • 我认为没有。这只是带有“Tabs + Swipe”的默认 Eclipse 项目的代码。
  • 好吧,你能看看fragment_main_dummy.xml 文件长什么样吗?根标签是FrameLayout 还是RelativeLayout(或者您可以将其发布在问题中)
  • 我添加了它。这能回答你的问题吗?

标签: android android-fragments android-button overlapping


【解决方案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);
}

【讨论】:

  • 这太棒了。有用。谢谢。所以我没有做错任何事,我只是从来没有给它一个规则,将它定位在之前的视图之下?我有标签来跟踪我点击了哪些,现在我应该能够使用标签或 ID 来跟踪我在 arraylist 中按下的按钮,对吧?
  • @Vasir 是的RelativeLayout 要求其子级指定放置规则,否则它们最终会在左上角一个在另一个之上。您可以使用 id 或标签,我会使用 ids,因为我认为它更干净。
猜你喜欢
  • 1970-01-01
  • 2018-05-28
  • 2017-08-17
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多