我想创建多个水平视图,例如flipkart主页

时间:2016-04-23 04:39:12

标签: android android-layout android-fragments android-studio

我必须创建像翻转购物车主页,所以我需要使用多个水平视图如何在android中以编程方式创建。在使用图像的垂直方向内的水平视图中,价格,产品名称将显示在android中。

感谢提前
我刚刚创建了如下所示的代码:

 public void shopimages()
{

   /* GradientDrawable drawable = new GradientDrawable(
   GradientDrawable.Orientation.TOP_BOTTOM,new int[] {R.color.shadow,Color.WHITE,R.color.shadow});
    drawable.setShape(GradientDrawable.RECTANGLE);*/
 //   drawable.setStroke(3, Color.BLACK);
    //drawable.setCornerRadius(8);

  //  drawable.setColor(0xFFFFFFFF); //white background
    // border.setStroke(1, 0xFF000000);



  //  categories = getResources().getStringArray(R.array.categories);

    /* child layout of horizontal*/
    LinearLayout[]  lLayout = new LinearLayout[8];

    for(int j=0;j<lLayout.length;j++ ) {
        lLayout[j] = new LinearLayout(getActivity());


        LinearLayout li=new LinearLayout(getActivity());
        li.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        li.setOrientation(LinearLayout.VERTICAL);

        /* Heading text for each horizontal images*/
        name=new TextView(getActivity());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0,150,0,0);
         name.setLayoutParams(params);
        name.setBackgroundColor(Color.parseColor("#FFFFFF"));
        name.setText("kani"+j);
        name.setPadding(10,10,10,10);
        name.setTextColor(Color.parseColor("#ff1319ff"));
         li.addView(name);
        li.setTop(50);
        root.addView(li);

        /*end*/

        /* child layout created dynamically*/
       // lLayout[j].setBackgroundDrawable(drawable);
        sv = new HorizontalScrollView(getActivity());
        sv.setHorizontalScrollBarEnabled(false);
        lLayout[j].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        lLayout[j].setOrientation(LinearLayout.HORIZONTAL);
         sv.addView(lLayout[j]);
        /*end*/


        t = new TextView[10];
        names=new TextView[10];
        price=new TextView[10];
        existingprice=new TextView[10];

        img = new ImageView[10];
        /*create the layout for textveiw and image view*/
        LinearLayout.LayoutParams image=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        image.topMargin=50;
        image.leftMargin=15;
        image.rightMargin=15;
        image.bottomMargin=10;
        image.width=200;
        image.height=150;


        LinearLayout.LayoutParams dim = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        dim.topMargin=20;
        dim.leftMargin=15;
        dim.rightMargin=15;
        dim.bottomMargin=10;
        dim.width=200;
        dim.height=80;
        LinearLayout.LayoutParams dim2 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        dim2.leftMargin=40;
        dim2.width=200;

        LinearLayout.LayoutParams dim3 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);




        /*end*/
        /*using for loop to run the rexxtview*/
        for (int i = 0; i < 10; i++) {
            LinearLayout imageVertical =new LinearLayout(getActivity());
            imageVertical.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            imageVertical.setOrientation(LinearLayout.VERTICAL);

            img[i] = new ImageView(getActivity());
            img[i].setPadding(8, 8, 8, 8);
            img[i].setScaleType(ImageView.ScaleType.CENTER_CROP);
            img[i].setImageResource(mThumbIds[i]);
            t[i] = new TextView(getActivity());
            img[i].setLayoutParams(image);
            t[i].setLayoutParams(dim);
            t[i].setGravity(Gravity.CENTER);
            t[i].setTextSize(14);
            t[i].setTextColor(Color.parseColor("#ff1319ff"));
            t[i].setText("YOHOHO: " + i);
            // name
            names[i] = new TextView(getActivity());
            names[i].setLayoutParams(dim2);
            names[i].setGravity(Gravity.CENTER);
            names[i].setTextSize(14);
            names[i].setTextColor(Color.parseColor("#ff1319ff"));

            /*names[i].setEllipsize (TextUtils.TruncateAt.END);*/
            names[i].setText("lightweight" + i);

            //horizontal linear layout
            LinearLayout pricelayout=new LinearLayout(getActivity());
            pricelayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));

            pricelayout.setOrientation(LinearLayout.HORIZONTAL);


            //price
            price[i] = new TextView(getActivity());
           // price[i].setLayoutParams(dim3);
            price[i].setGravity(Gravity.CENTER);
            price[i].setTextSize(9);
            price[i].setTextColor(Color.parseColor("#ff1319ff"));
            price[i].setText("Rs.2000");

            // existing price
            existingprice[i] = new TextView(getActivity());
            /*existingprice[i].setLayoutParams(dim3);*/
            existingprice[i].setGravity(Gravity.CENTER);
            existingprice[i].setTextSize(9);
            existingprice[i].setTextColor(Color.parseColor("#ff1319ff"));
            existingprice[i].setPaintFlags(existingprice[i].getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
            existingprice[i].setText("Rs.5340");

            pricelayout.addView(price[i]);
            pricelayout.addView(existingprice[i]);



            imageVertical.addView(img[i]);

            imageVertical.addView(t[i]);
            imageVertical.addView(names[i]);
            imageVertical.addView(pricelayout);
           // imageVertical.addView(existingprice[i]);
             lLayout[j].addView(imageVertical);


        }
        /*end*/

        /* parent layout we can add the child view */
        root.addView(sv);

    }

0 个答案:

没有答案