我在滚动视图(由<include>添加)中添加了许多视图。这些布局如下:
<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不像预期的那样工作。
发布于 2017-07-25 10:24:34
你可以用多种方式获得屏幕的宽度和高度
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}getScreenHeight()将返回总高度
您可以使用此高度并按以下方式设置参数:
desired_height_params = getScreenHeight() *2/3
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
desired_height_params
);
linLayout.setLayoutParams(lp);https://stackoverflow.com/questions/45299782
复制相似问题