将 FrameLayout 分配为父 RelativeLayout 的一半

Assigning FrameLayout as half of parent RelativeLayout

我在相对布局中有一个框架布局,其他所有内容都位于它下面。 如何让这个框架布局占整个屏幕的一半?

我试过重量,但没有任何改变..

<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 

I have a frame layout inside a relative layout, that everything else is positioned below it

那么您 parent 使用了错误的布局。您应该将 RelativeLayout 替换为 LinearLayout,将 FrameLayout 高度设置为 match_parentandroid:layout_weight="1",然后将您想要的所有内容都包装在任何需要的内容下方(相对、线性等)确保它还设置了高度和 layout_weight 如上所述。然后你会得到50%-50%的高度分割

<?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>

权重仅适用于线性布局,不适用于相对布局

    <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>

给每个布局高度 0dp 和权重 50 并给线性布局权重总和 100

Marcin 是对的,Weight 属性 仅当您将父布局作为线性布局时才有效,并且为了使您的框架布局覆盖半个屏幕,您必须将两个布局作为该线性布局的子布局并等分。

例如

<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>

希望对您有所帮助。