[英]android LinearLayout to RelativeLayout
我正在为 android 制作应用程序,但遇到了问题。 我想将我的 XML 代码从 LinearLayout 更改为 RelativeLayout。 在一行中,我想要 4 个进度条。 我可以用 LinearLayout 做到这一点,但 RelativeLayout 不是。
代码:
<LinearLayout
android:id="@+id/lineral"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/kcal"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_marginLeft="5dp"
android:progress="25"
android:layout_weight="1"
android:progressTint="#69f0ae"
/>
<ProgressBar
android:id="@+id/protein"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_marginLeft="5dp"
android:progress="25"
android:layout_toRightOf="@id/kcal"
android:layout_weight="1"
android:progressTint="#03A9F4"
/>
<ProgressBar
android:id="@+id/fat"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/protein"
android:layout_weight="1"
android:progress="25"
android:progressTint="#FFEB3B"
/>
<ProgressBar
android:id="@+id/carbohydrates"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/fat"
android:layout_weight="1"
android:progress="25"
android:progressTint="#F44336"
/>
</LinearLayout>
如果要以相同的宽度显示所有进度条,则必须使用 Linearlayout
如果您使用的是相对布局,则必须在所有 ProgressBar 中设置宽度
您正在使用android:layout_weight=1设置进度条的权重,但这是 LinearLayout.LayoutParams 的属性,而不是 RelativeLayout.LayoutParams 的属性。
替代解决方案
您可以在 Recycler View 中使用 Horizontally-oriented LinearLayout Manager,并将每个 RelativeLayout 放置在其 Adapter 的每个项目中。 检查链接
如果您的 RelativeLayouts 设置为固定的宽度和高度,即屏幕的大小,您可以从 DisplayMatrics 获得,那就可以了。 检查链接
如果您的 RelativeLayouts 的内容不同,那么您可以使用 getItemViewType() 方法。 检查链接
<androidx.constraintlayout.widget.ConstraintLayout 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">
<ProgressBar
android:id="@+id/oP"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="25dp"
android:progress="25"
android:progressTint="#69f0ae"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintRight_toLeftOf="@+id/oP2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintLeft_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Progress 1"
app:layout_constraintEnd_toEndOf="@+id/oP"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="@+id/oP"
app:layout_constraintTop_toBottomOf="@+id/oP" />
<ProgressBar
android:id="@+id/oP2"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="25dp"
android:progress="25"
android:progressTint="#69f0ae"
app:layout_constraintRight_toLeftOf="@+id/oP3"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@id/oP"
app:layout_constraintLeft_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Progress 2"
app:layout_constraintEnd_toEndOf="@+id/oP2"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="@+id/oP2"
app:layout_constraintTop_toBottomOf="@+id/oP2" />
<ProgressBar
android:id="@+id/oP3"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="0dp"
app:layout_constraintRight_toLeftOf="@+id/oP4"
android:layout_height="25dp"
android:progress="25"
android:progressTint="#69f0ae"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@id/oP2"
app:layout_constraintLeft_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Progress 2"
app:layout_constraintEnd_toEndOf="@+id/oP3"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="@+id/oP3"
app:layout_constraintTop_toBottomOf="@+id/oP3" />
<ProgressBar
android:id="@+id/oP4"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="25dp"
android:progress="25"
android:progressTint="#69f0ae"
app:layout_constraintLeft_toRightOf="@id/oP3"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Progress 4"
app:layout_constraintEnd_toEndOf="@+id/oP4"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="@+id/oP4"
app:layout_constraintTop_toBottomOf="@+id/oP4" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.