制作scrollview 1/3尺寸的孩子

时间:2017-06-01 10:02:21

标签: android scrollview android-linearlayout android-layout-weight

我有这个: enter image description here

我的问题是我有一个带有scrollview和Linearlayout的Linearlayout包装器..我希望每个项目都是scrollview高度的1/3,无论我放入多少项目..

当我向Linearlayout添加项目时,它是线性布局,如果我添加3个“项目”它几乎没有..但如果我添加8个项目,它们都非常小且可滚动。这就是我想要的,但我希望它们的高度为scrollview / linearlayout的1/3。

我希望它能够与我在第二个布局中的按钮一样大。

我似乎无法做到正确,希望它只是一个我拒绝看到的简单解决方案。

到目前为止我的代码:

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:orientation="vertical"
                android:layout_weight="75">
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/scrollView2"
                    android:layout_gravity="center_horizontal"
                    android:fillViewport="true"
                    android:scrollbarSize="5dp"
                    android:fadeScrollbars="false"
                    android:layout_marginRight="5dp"
                    android:layout_marginLeft="5dp"
                    android:scrollbarThumbVertical="@drawable/custom_scroll_style">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/coats"
                        android:orientation="vertical"
                        android:weightSum="75">

                 </LinearLayout>
                </ScrollView>
            </LinearLayout>

这是我将'children'添加到scrollview包含的布局

的方法
LinearLayout w0 = new LinearLayout(this);
        LinearLayout w1 = new LinearLayout(this);
        ImageView w2 = new ImageView(this);
        TextView w3 = new TextView(this);
        TextView w4 = new TextView(this);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 25);
        params.setMargins(pixelToDp(0),pixelToDp(5),pixelToDp(0),pixelToDp(5));
        w0.setLayoutParams(params);
        w0.setOrientation(LinearLayout.HORIZONTAL);
        w0.setClickable(true);

        w0.setBackgroundColor(Color.argb(100, 100, 100, 100));
        w0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                for (int i = 0; i < LLCoats.getChildCount(); i++) {
                    View vv = LLCoats.getChildAt(i);
                    vv.setSelected(false);
                    vv.setBackgroundColor(Color.argb(100, 100, 100, 100));
                }
                LinearLayout LL = (LinearLayout)LLCoats.getChildAt(i);
                TextView TV = (TextView)LL.getChildAt(1);
                TV.requestFocus();

                if(TV.getError() != null)
                {
                    Snackbar snackbar = Snackbar
                            .make(findViewById(android.R.id.content), ""+TV.getError(), Snackbar.LENGTH_LONG);
                    snackbar.show();
                }



                v.setSelected(true);
                v.setBackgroundColor(Color.argb(255,255,255,255));
            }
        });

        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 2);
        w1.setLayoutParams(params2);
        w1.setOrientation(LinearLayout.VERTICAL);
        w1.setClickable(false);

        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
        w2.setLayoutParams(params3);
        w2.setScaleType(ImageView.ScaleType.FIT_CENTER);      

        LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
        w3.setLayoutParams(params4);
        w3.setTextSize(25f);
        w3.setGravity(Gravity.CENTER);
        w3.setClickable(false);

        LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
        w4.setLayoutParams(params5);
        w4.setTextSize(45f);
        w4.setGravity(Gravity.CENTER);
        w4.setClickable(false);

        w1.addView(w2);
        w1.addView(w3);

        w0.addView(w1);
        w0.addView(w4);

        LLCoats.addView(w0);

UPDATE:

现在变得非常奇怪,因为我找到了一种计算高度的方法并将其添加:

DisplayMetrics display = this.getResources().getDisplayMetrics();

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, pixelToDp((int)(display.heightPixels* .8 * 0.25)));

这在我的PI64和华硕平板电脑上运行得非常好,但是我的手机让高度100%与滚动视图一样,怎么回事?

1 个答案:

答案 0 :(得分:0)

我的设计是这样的,获得屏幕尺寸* .8,因为屏幕用作弹出窗口,然后是0.25,因为项目是屏幕的1/4,最后是-10,用于我的边距

DisplayMetrics display = this.getResources().getDisplayMetrics();        

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int)(display.heightPixels* .8 * 0.25 - pixelToDp(10)));       

        params.setMargins(pixelToDp(0),pixelToDp(5),pixelToDp(0),pixelToDp(5));