android LinearLayout到RelativeLayout

android LinearLayout到RelativeLayout,android,Android,我正在为android做一个应用程序,我遇到了问题。 我想将XML代码从LinearLayout更改为RelativeLayout。 在一行中,我要4个进度条。我可以用线性布局来做,但相对来说你不行 效果: 代码: 如果要以相同的宽度显示所有进度条,必须使用Linearlayout 如果您使用的是相对布局,则在所有进度条中,您必须设置宽度,您使用的是android:layout_weight=1来设置进度条的权重,但这是LinearLayout.LayoutParams的属性,而不是Relat

我正在为android做一个应用程序,我遇到了问题。 我想将XML代码从LinearLayout更改为RelativeLayout。 在一行中,我要4个进度条。我可以用线性布局来做,但相对来说你不行

效果:

代码:


如果要以相同的宽度显示所有进度条,必须使用Linearlayout


如果您使用的是相对布局,则在所有进度条中,您必须设置宽度,您使用的是android:layout_weight=1来设置进度条的权重,但这是LinearLayout.LayoutParams的属性,而不是RelativeLayout.LayoutParams的属性

替代解决方案

  • 您可以在“回收器”视图中使用水平定向的LinearLayout管理器,并将每个RelativeLayout放置在其适配器的每个项目中

  • 如果您的RelativeLayouts设置为一个固定的宽度和高度,即屏幕大小,您可以从DisplayMatrics获得,那就可以了

  • 如果RelativeLayouts的内容不同,则可以使用getItemViewType()方法

  • 所需的约束布局视图

    
    

    最好仅根据您的要求使用线性布局。如果您可以使用线性布局,为什么需要将其更改为相对布局?我想在每个进度栏下方添加一些文本?这并不能回答问题,如果您没有注意到它是线性布局,那么您也应该在他的布局文件的comment.btw中告诉它。。不是相对布局。是的,我知道他使用的是线性布局。但他想用相对布局来代替它。请检查问题Sokej,但我希望每个进度条添加进度条的文本名称值,我不知道它如何使用LinearLayoutSimple解决方案:为此,您必须为每个进度条创建一个新视图。[LinearLayout->RelativeLayout[progressbar和下面的文本]]。但建议的解决方案是使用约束布局。
          <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>
    
    <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" />