0

我有这样的: enter image description here使子1/3大小

我的问题是,我有一个的LinearLayout包装用滚动型和里面的LinearLayout ..我想每一个项目是的1/3 scrollviews的高度,无论我放入多少物品..

当我将项目添加到Linearlayout它是linearlayouts,如果我添加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> 

这是我加入 '孩子' 的布局由滚动型包裹

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和华硕平板电脑上运行得非常好,但是我的手机的高度与scrollview一样高达100%,情况如何?

回答

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));