Android:布局总是2/3屏幕
问题描述:
我有很多视图,我在滚动视图中添加(通过<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
不能按预期工作。
答
你可以得到宽度和多张的方式
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);
为什么不以编程方式获取设备的高度并设置为您的愿望视图w ith 2/3的比例。 –
@AdnanMaqbool感谢您的评论,但我需要发布另一个问题如何做到这一点,说实话。你能告诉我方式吗? – soommy12
检查我的更新答案。希望能帮助到你 –