简体   繁体   中英

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. Just replace MATCH_PARENT with WRAP_CONTENT as shown below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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