簡體   English   中英

在相對布局中添加多個水平視圖

[英]Adding Multiple Horizontal Views In A Relative Layout

我需要的是

第一節

水平滾動視圖

第二節

水平滾動視圖

而且它可以是動態的,沒有節,因此我需要動態生成它們,但是如果我使用此代碼,則只有最后一個水平滾動視圖起作用

休息其他不工作

我的代碼;

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;


       }
       }       

發生這種情況的原因是您使用了MATCH_PARENT參數作為Horizo​​ntalScrollView的高度。 只需將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