首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将FrameLayout指定为父RelativeLayout的一半

将FrameLayout指定为父RelativeLayout的一半
EN

Stack Overflow用户
提问于 2015-08-02 13:18:21
回答 3查看 5K关注 0票数 4

我在一个相对的布局中有一个框架布局,其他的东西都在它下面。如何使这个帧布局占据整个屏幕的一半?

我试过体重,但这改变不了什么。

代码语言:javascript
运行
复制
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/mainLayoutStyle">

<FrameLayout 
       android:id="@+id/codeLayout"
       style="@style/codeLayoutLargeStyle">   

        <TextView 
            android:id="@+id/DisplayTextView"
            style="@style/DisplayTextViewLargeStyle"/>
</FrameLayout>
......
.....
   <LinearLayout 
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-02 13:22:19

我在一个相对的布局中有一个框架布局,其他的东西都在它下面。

然后,您使用错误的布局作为您的父级。您应该将RelativeLayout替换为LinearLayout,将FrameLayout高度设置为match_parentandroid:layout_weight="1",然后将所需的任何东西(相对的、线性的)包装到所需的以下,以确保它也得到了设置的高度和上面的layout_weight。然后你会得到50%-50%的高度分裂。

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_weight="1"
        android:background="#ff0000"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <RelativeLayout
        android:layout_weight="1"
        android:background="#0000ff"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

票数 4
EN

Stack Overflow用户

发布于 2015-08-02 13:28:44

权重只适用于线性布局,而不是相对布局。

代码语言:javascript
运行
复制
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/mainLayoutStyle"
    android:weightSum="100">

    <FrameLayout 
        android:id="@+id/codeLayout1"
        style="@style/codeLayoutLargeStyle"
android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="50">   

    </FrameLayout>


    <FrameLayout 
        android:id="@+id/codeLayout2"
        style="@style/codeLayoutLargeStyle"
android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="50">   

    </FrameLayout>

    </LinearLayout>

给出每个布局高度0 0dp和重量50,并给出线性布局权重之和为100。

票数 0
EN

Stack Overflow用户

发布于 2015-08-02 13:39:32

Marcin是对的,只有当您将父布局作为线性布局,而要使框架布局覆盖半屏幕时,权重属性才会有效,您必须将两个布局作为该线性布局的子布局,并将其除以相等的权重。

e.g

代码语言:javascript
运行
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
    android:id="@+id/you_frame_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#00ff00">

</FrameLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

</RelativeLayout>

希望这对你有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31772274

复制
相关文章

相似问题

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