简体   繁体   English

在相对布局中添加多个水平视图

[英]Adding Multiple Horizontal Views In A Relative Layout

What I need Is 我需要的是

Section 1 第一节

Horizontal Scroll View 水平滚动视图

Section 2 第二节

Horizontal Scroll View 水平滚动视图

And It Can be Dynamic No Of Sections So I Need To Generate Them Dynamically But If I Use This Code Only The Last Horizontal Scroll View WOrks 而且它可以是动态的,没有节,因此我需要动态生成它们,但是如果我使用此代码,则只有最后一个水平滚动视图起作用

Rest Other Do Not Work 休息其他不工作

MY Code; 我的代码;

public class HomeFragment extends Fragment 
    {
      View rootView;
      int i = 0;

    int j =0;
    public HomeFragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

         rootView = inflater.inflate(R.layout.fragment_home, container, false);
        RelativeLayout rl=(RelativeLayout)rootView.findViewById(R.id.relativelay);
   HorizontalScrollView sv = new HorizontalScrollView(getActivity().getApplicationContext()) ;

       sv =  hsview(100);
        rl.addView(sv);
    sv = hsview(300);
        rl.addView(sv);

              return rootView;

    }


    public HorizontalScrollView hsview(int k)
{



        HorizontalScrollView sv = new HorizontalScrollView(getActivity().getBaseContext());
      sv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        LinearLayout ll = new LinearLayout(getActivity().getApplicationContext());
        LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        margin.setMargins(0,k,0,0);
       ll.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        ll.setOrientation(LinearLayout.HORIZONTAL);

        for (i = k; i < 310; i++) {
            Button b = new Button(getActivity().getApplicationContext());
            b.setText("Button " + i);
          //  b.setLayoutParams(margin);
            ll.addView(b);
        }

        sv.addView(ll);
        return sv;


       }
       }       

This might happen because you are using MATCH_PARENT parameter for height of the HorizontalScrollView. 发生这种情况的原因是您使用了MATCH_PARENT参数作为Horizo​​ntalScrollView的高度。 Just replace MATCH_PARENT with WRAP_CONTENT as shown below 只需将MATCH_PARENT替换为WRAP_CONTENT,如下所示

sv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM