首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:布局总是2/3屏幕

Android:布局总是2/3屏幕
EN

Stack Overflow用户
提问于 2017-07-25 09:53:48
回答 1查看 586关注 0票数 0

我在滚动视图(由<include>添加)中添加了许多视图。这些布局如下:

代码语言:javascript
复制
<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布局中的:

代码语言:javascript
复制
<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不像预期的那样工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-25 10:24:34

你可以用多种方式获得屏幕的宽度和高度

代码语言:javascript
复制
public static int getScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;
}

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

getScreenHeight()将返回总高度

您可以使用此高度并按以下方式设置参数:

代码语言:javascript
复制
desired_height_params = getScreenHeight() *2/3

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT, 
    desired_height_params
);
linLayout.setLayoutParams(lp);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45299782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档