2016-04-26 42 views
0

我的佈局如下,如果我刪除android:weightSum="3"然後一切正常,但如果我在的LinearLayout把android:weightSum="3"(所以每個TextView的具有1/3寬度的屏幕),則Horizo​​ntalScrollView似乎不能滾動,任何人都知道這是爲什麼? THXHorizo​​ntalScrollView無法使用android:weightSum?

<HorizontalScrollView 
      android:id="@+id/bottom_bar_id" 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:fillViewport="true" 
      android:scrollbars="none"> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:orientation="horizontal" 
       android:weightSum="3"> 

       <TextView 
        android:id="@+id/oneMonth" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:layout_marginLeft="15dp" 
        android:layout_marginRight="15dp" 
        android:textColor="@color/text_title" 
        android:text="one"/> 


       <TextView 
        android:id="@+id/threeMonth" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:layout_marginLeft="15dp" 
        android:layout_marginRight="15dp" 
        android:textColor="@color/text_title" 
        android:text="two"/> 

       <TextView 
        android:id="@+id/halfYear" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:layout_marginLeft="15dp" 
        android:layout_marginRight="15dp" 
        android:textColor="@color/text_title" 
        android:text="three"/> 

       <TextView 
        android:id="@+id/oneYear" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:layout_marginLeft="15dp" 
        android:layout_marginRight="15dp" 
        android:textColor="@color/text_title" 
        android:text="four"/> 


       <TextView 
        android:id="@+id/thisYear" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:layout_marginLeft="15dp" 
        android:layout_marginRight="15dp" 
        android:textColor="@color/text_title" 
        android:text="five"/> 

       <TextView 
        android:id="@+id/beginNow" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:layout_marginLeft="15dp" 
        android:layout_marginRight="15dp" 
        android:textColor="@color/text_title" 
        android:text="six"/> 
      </LinearLayout> 
     </HorizontalScrollView> 
+1

你給weigthsum 3,但你的孩子爲什麼要使用weigthsum = 3時,要將它劃分至6份超限3 –

+0

變化weightsum 3至6 –

+0

?要麼給機器人:layout_weight =」 5或給weigthsum = 6到您的線性佈局 –

回答

0

我做到了通過這種方式:

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels/YourChildWeight, LinearLayout.LayoutParams.MATCH_PARENT); 
parentView.addView(childView, params); 

你需要給ChildWeight

希望這會幫助你。

+0

但是爲什麼如果'機器人:weightSum =「3」'設置好的,在horizo​​ntalscrollview不能滾動? –

+0

@TigerWang,需要修復顯示寬度,因爲水平滾動視圖沒有X限制 –

0

王虎王,

我也在尋找解決方案。

我想用Horizo​​ntalScrollView和具有相同大小的10個按鈕,但一次顯示5點相等大小的按鈕,而無需使用WRAP_CONTENT所有按鈕。

對於按鈕,我想指定layout_weight,weightSum和layout_width = 「0dp」。

讓我知道你是否找到解決方案。

感謝, 阿肖克

+0

對不起,我也沒有解決方案。 –