2017-07-25 119 views
0

我有很多視圖,我在滾動視圖中添加(通過<include>)。這些佈局是這樣的:Android:佈局總是2/3屏幕

<RelativeLayout 
     android:id="@+id/one" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="18dp" 
     android:layout_marginRight="18dp" 
> 
</RelativeLayout> 

這是我包括那些佈局到fragment佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
> 
    <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> 
     <LinearLayout android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:orientation="vertical" 
     > 
      <include layout="@layout/one"/> 
      <include layout="@layout/two"/> 
      <include layout="@layout/three"/> 
     </LinearLayout> 
     </ScrollView> 
</LinearLayout> 

如何讓這3個佈局採取高度始終在屏幕的2/3。我希望它始終在每個設備的2/3屏幕上。 layout_weight不能按預期工作。

+0

爲什麼不以編程方式獲取設備的高度並設置爲您的願望視圖w ith 2/3的比例。 –

+0

@AdnanMaqbool感謝您的評論,但我需要發佈另一個問題如何做到這一點,說實話。你能告訴我方式嗎? – soommy12

+0

檢查我的更新答案。希望能幫助到你 –

回答

0

你可以得到寬度和多張的方式

public static int getScreenWidth() { 
    return Resources.getSystem().getDisplayMetrics().widthPixels; 
} 

public static int getScreenHeight() { 
    return Resources.getSystem().getDisplayMetrics().heightPixels; 
} 

getScreenHeight()屏幕的高度將返回總高度

你可以用這個高度並設置PARAMS如下:

desired_height_params = getScreenHeight() *2/3 

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT, 
    desired_height_params 
); 
linLayout.setLayoutParams(lp);