我在相对布局内直接有两个线性布局。我希望第一个LinearLayout占据75%的高度,接下来的25%。我该如何实现?
例如
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:weightSum="100"
android:id="@+id/contentMainLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="75"
android:id="@+id/linearLayout1">
<ScrollView
android:id="@+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/mainScrollLL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="25"
android:layout_marginBottom="20dp"
android:layout_below="@+id/mainScrollParentLL"
android:orientation="horizontal">
<Button
android:id="@+id/startTrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Trade"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Stop Trade"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</RelativeLayout>
我希望linearLayout1使用75%的高度,而linearLayout2使用25%的高度。没有为LinearLayout定义layout_weight,因此显然它不起作用。
有什么办法可以实现?
这似乎是一个很常见的情况,所以我几乎可以肯定已经问过这个问题了。但我似乎找不到。如果这是重复的问题,请指出原始问题,我将关闭它。
您可以尝试下面的代码-您需要将RelativeLayout
更改为LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/contentMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="75">
<ScrollView
android:id="@+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/mainScrollLL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="25"
android:orientation="horizontal">
<Button
android:id="@+id/startTrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Trade"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Stop Trade"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</LinearLayout>
请尝试一下...
<LinearLayout
android:id="@+id/contentMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:id="@+id/linearLayout1"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="75">
<ScrollView
android:id="@+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/mainScrollLL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<Button
android:id="@+id/startTrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Trade"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Stop Trade"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</LinearLayout>
weightSum不是相对布局的属性。您必须使用线性布局作为父布局,然后才能实现目标。