gpt4 book ai didi

java - 将 Constraintlayout 更改为 LinearLayout

转载 作者:行者123 更新时间:2023-12-01 16:33:11 26 4
gpt4 key购买 nike

:)

我想将我的 constraintlayout 更改为 LinearLayout。我尝试了绿色(第二个区域),效果非常好。不幸的是,顶部部分的效果不如底部部分。问题是红色区域总是占据整个区域并且不会到达工具栏。如何使红色区域的高度仅达到工具栏+距离?我不想在工具栏 XML 中更改“更多”内容,否则所有其他页面上的布局都是错误的。是否有一个选项允许 android: layout_height = "match_parent-toolbar"

我希望它最终看起来像第二张图片。红色区域应该与工具栏保持一定距离,并且距离最大。因此,无论红色区域中有多少内容,到目前为止,它都应该尽可能大。我怎样才能做到这一点?我真的很期待有用的答案!提前致谢。

我目前的状态:

我想要实现的目标:

我的尝试

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ececec"
android:orientation="horizontal">

<androidx.cardview.widget.CardView
android:id="@+id/cardViewMiddle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:elevation="8dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:gravity="center"
app:cardBackgroundColor="@color/Red"
app:cardCornerRadius="15dp">

</androidx.cardview.widget.CardView>

</LinearLayout>

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ececec">

<androidx.cardview.widget.CardView
android:id="@+id/cardViewMiddle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:elevation="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:gravity="center"
app:cardBackgroundColor="@color/Red"
app:cardCornerRadius="15dp"
app:layout_constraintBottom_toTopOf="@+id/cardViewCheckout"
app:layout_constraintTop_toTopOf="parent">

</androidx.cardview.widget.CardView>

<androidx.cardview.widget.CardView
android:id="@+id/cardViewCheckout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="10dp"
android:elevation="8dp"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="15dp">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5sp"
android:layout_marginBottom="5sp"
android:layout_gravity="center">

<TextView
android:id="@+id/textViewSumme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Summe "
android:layout_gravity="center"/>
<TextView
android:id="@+id/textViewSumme2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0,00"
android:layout_gravity="center"/>

</LinearLayout>

<Button
android:id="@+id/btn_user_geldaufladen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/button_order_checkout"
android:backgroundTint="#04C3B1"
android:elevation="16dp"
android:text="AUFLADEN"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:layout_marginBottom="10sp"/>

</LinearLayout>



</androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>

工具栏

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UserHomeActivity">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

<include layout="@layout/user_content_main"


/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

最佳答案

试试这个。

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ececec"
android:padding="16dp"
android:orientation="vertical"
android:layout_below="@+id/appBar"
android:weightSum="10">

<androidx.cardview.widget.CardView
android:id="@+id/cardViewMiddle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:elevation="8dp"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:gravity="center"
app:cardBackgroundColor="#f00"
app:cardCornerRadius="15dp"
app:layout_constraintBottom_toTopOf="@+id/cardViewCheckout"
app:layout_constraintTop_toTopOf="parent">

</androidx.cardview.widget.CardView>

<androidx.cardview.widget.CardView
android:id="@+id/cardViewCheckout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="10dp"
android:elevation="8dp"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="15dp">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5sp"
android:layout_marginBottom="5sp"
android:layout_gravity="center">

<TextView
android:id="@+id/textViewSumme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Summe "
android:layout_gravity="center"/>
<TextView
android:id="@+id/textViewSumme2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0,00"
android:layout_gravity="center"/>

</LinearLayout>

<Button
android:id="@+id/btn_user_geldaufladen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/colorPrimaryDark"
android:backgroundTint="#04C3B1"
android:elevation="16dp"
android:text="AUFLADEN"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:layout_marginBottom="10sp"/>

</LinearLayout>



</androidx.cardview.widget.CardView>


</LinearLayout>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/appBar"
>

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="?attr/colorPrimary"
/>

</com.google.android.material.appbar.AppBarLayout>
</RelativeLayout>

enter image description here

关于java - 将 Constraintlayout 更改为 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62008171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com